C++ BFS solution


  • 0
    G

    '''

    class Solution
    {
    public:

    int maxDepth(TreeNode* root)

    {

      int count=0;
      if(root==nullptr)
          return count;
      else
      {
        queue<TreeNode*> q;
        q.push(root);
        while(!q.empty())
        {
            
            int s=q.size();
            for(int i=0;i<s;i++)
            {
                TreeNode* t=q.front();
                q.pop();
                if(t->left) q.push(t->left);
                if(t->right) q.push(t->right);   
            }
            ++count;   
        }
        return count;
      }
    

    }
    };


Log in to reply
 

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