104.Maximum Depth of Binary Tree
程序员文章站
2022-05-18 19:36:52
...
problems:
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Note: A leaf is a node with no children.
Example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its depth = 3.
tips:
求二叉树的最大深度。对于每个根节点,求得它左子树和右子树的最大深度然后加1即可。
solution:
class Solution {
public:
int maxDepth(TreeNode* root) {
int i,j;
if(root == NULL) return 0;
i = maxDepth(root->left)+1;
j = maxDepth(root->right)+1;
if(i>=j) return i;
else return j;
}
};
推荐阅读
-
LC 297 Serialize and Deserialize Binary Tree
-
leetcode笔记:Invert Binary Tree
-
Convert Sorted Array to Binary Search Tree
-
minimum-depth-of-binary-tree
-
cf438E. The Child and Binary Tree(生成函数 多项式开根 多项式求逆)
-
【leetcode】-700. Search in a Binary Search Tree 查找二叉搜索树
-
Leetcode——108. Convert Sorted Array to Binary Search Tree
-
Leetcode 108. Convert Sorted Array to Binary Search Tree
-
21天刷题计划之17.1—maximum-depth-of-binary-tree(二叉树的最大深度)(Java语言描述)
-
21天刷题计划之18.1—balanced-binary-tree(平衡二叉树)(Java语言描述)