104. 二叉树的最大深度
104. 二叉树的最大深度
题解
思路就是递归,分治法,分而治之
跟着代码想这个图即可理解
代码
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func maxDepth(root *TreeNode) int {
if root==nil{
return 0
}
left:=maxDepth(root.Left)
right:=maxDepth(root.Right)
if left>right{
return left+1
} else {
return right+1
}
}