easy bfs method in c++


  • 0
    J

    class Solution {
    public:
    int minDepth(TreeNode* root) {
    //int res=INT_MAX;
    int cnt=0;
    queue<TreeNode*> q;
    if(!root){
    return 0;
    }
    q.push(root);
    while(!q.empty()){
    int n=q.size();
    cnt++;
    for(int i=0;i<n;i++){
    TreeNode* curt=q.front();
    q.pop();
    if(!curt->left&&!curt->right)
    return cnt;
    if(curt->left){
    q.push(curt->left);
    }
    if(curt->right){
    q.push(curt->right);
    }
    }
    }
    return cnt;
    }
    };


Log in to reply
 

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