题目分析:[[EVD]] - 剑指 Offer 55 - I. 二叉树的深度
https://leetcode-cn.com/problems/er-cha-shu-de-shen-du-lcof/
简单描述:
- 求二叉树的深度
限制🚫
- 节点总数 <= 10000
示例:
3
/ \
9 20
/ \
15 7
输入:[3,9,20,null,null,15,7]
输出:3
解题思路:
思路:
- #DFS #BFS 树深=max(根的左子树,根的右子树)+1;
效率:
- 时间复杂度
- 空间复杂度
代码:
class Solution
{
public:
int maxDepth(TreeNode *root)
{
if (root == NULL)
return 0;
return max(maxDepth(root->left), maxDepth(root->right)) + 1;
}
};