Maximum Depth of Binary Tree


  • 0
    S

    static int _depth = 1;

    class Solution {
    public:
    int maxDepth(TreeNode *root) {
    if (!root)
    return 0;
    DepthTravle(root, 1);
    return _depth;
    }
    void DepthTravle(TreeNode *node, int depths){
    if (_depth <= depths)
    {
    _depth = depths;
    }
    if (node->left) DepthTravle(node->left, depths + 1);
    if (node->right) DepthTravle(node->right, depths + 1);
    }
    };

    Input: {0,0,0,0,#,#,0,#,#,#,0}
    Output: 5
    Expected: 4
    but i test in my IDE is output 4
    {0,0,0,0,#,#,0,#,#,#,0}
    is that ?
    0
    | |
    0 0
    | |
    0 0
    |
    0


  • 0
    C

    have you solved your problems? I got the same problem with you and I don`t understand why


Log in to reply
 

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