LeetCode---104. Maximum Depth of Binary Tree

  • 内容
  • 评论
  • 相关

递归遍历即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//url:https://leetcode.com/problems/maximum-depth-of-binary-tree/description/
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
 
    void maxDepth(TreeNode* root,int& depth,int level){
        if(!root)
            return ;
        level++;
        depth=depth>level?depth:level;
        if(root->left)
            maxDepth(root->left,depth,level);
        if(root->right)
            maxDepth(root->right,depth,level);
    }
 
    int maxDepth(TreeNode* root) {
        int depth=0;
        maxDepth(root,depth,0);
        return depth;
    }
};

原创文章,转载请注明: 转载自royalchen的博客

本文链接地址: LeetCode---104. Maximum Depth of Binary Tree

评论

0条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注