// 从叶子节点考虑,叶子节点子节点为null,所以叶子节点return 1,也就是叶子节点深度为1,从叶子节点往上数树的max深度
int maxDepth(TreeNode* root) {
if (root == NULL)
return 0;
int left = 0;
int right = 0;
left += maxDepth(root->left)+1;
right += maxDepth(root->right)+1;
return left > right?left:right;
}
leetcode104. Maximum Depth of Binary Tree
最新推荐文章于 2023-11-09 13:10:38 发布