题目:
给定一个二叉树 root ,返回其最大深度。

二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
class Solution { public: int maxDepth(TreeNode* root) { if(!root){return0;} return max(maxDepth(root->left),maxDepth(root->right))+1; } };
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class Solution { public: int maxDepth(TreeNode* root) { int result = 0; function<void(TreeNode* ,int )> def = [&result,&def](TreeNode* root,int count){ if(!root){ return; } count +=1; result = max(result,count); def(root->left,count); def(root->right,count); }; def(root,0); return result; } };
|