BFS imspired solution


  • 0

    Here is my BFS inspired solution, but failed to pass the big cases when the count of the nodes exceeds

    class Solution {
    public:
        int countNodes(TreeNode* root) {
            deque<TreeNode*> d;
            int result=0;
            if(!root)  return result;
            
            d.push_back(root);
            while(!d.empty()){
                result++;
                TreeNode* temp=d.front();
                d.pop_front();
                if(temp->left)  d.push_back(temp->left);
                if(temp->right) d.push_back(temp->right);
            }
            return result;
        }
    };

Log in to reply
 

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