- 104. Maximum Depth of Bianry Tre
- Leetcode PHP题解--D41 104. Maximum
- 104. Maximum Depth of Binary Tre
- 104. Maximum Depth of Binary Tre
- 104. Maximum Depth of Binary Tre
- 104. Maximum Depth of Binary Tre
- 104. Maximum Depth of Binary Tre
- 104. Maximum Depth of Binary Tre
- 104. Maximum Depth of Binary Tre
- 104. Maximum Depth of Binary Tre
题目
求二叉树的最大深度
解析
二叉树的最大深度,是左子树深度加 1 和右子树深度加 1 的最大值。即 f(node) = max(f(node.left), f(node.right)) +1
伪代码
return max(f(node.left), f(node.right)) + 1
代码
func maxDepth(root *TreeNode) int {
if root == nil {
return 0
}
return max(maxDepth(root.Left), maxDepth(root.Right)) + 1
}
func max(a, b int) int {
if a > b {
return a
}
return b
}

网友评论