C++ solution, BFS with a queue.


  • 0
    1
    int min=-1;
    int minDepth(TreeNode* root) {
        queue<TreeNode*> que;
        if(!root)  return 0;
        int depth=0;
        TreeNode*level=NULL;
        que.push(level);
        que.push(root);
        while(!que.empty()){
            TreeNode*tmp=que.front();
            que.pop();
            if(!tmp){
                 ++depth;
                 que.push(level);
                 continue;
            }
            if(!tmp->left and !tmp->right){
                    return depth;
            }
            else{
                if(tmp->left)
                   que.push(tmp->left);
                if(tmp->right)
                   que.push(tmp->right);
            }
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.