6ms C++ solution using BFS


  • 0
    Z
        //BFS
        if(root == NULL) return 0;
        int counter = 0;
        queue<TreeNode*> q;
        q.push(root);
        
        while(!q.empty()){
            int size = q.size();
            while(size--){ //layer
                TreeNode* t = q.front();
                q.pop(); //init is root pop out
                if(t->left != NULL) q.push(t->left);
                if(t->right != NULL) q.push(t->right);
            }    
            counter++;
        }
        return counter;

Log in to reply
 

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