传送到leetcode: leetcode-104
123456789101112131415161718192021222324252627282930313233
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */// dfs实现func maxDepth(root *TreeNode) int { max := 0 startDeep := 0 var dfs func(node *TreeNode, startDeep int) dfs = func(node *TreeNode, startDeep int) { if node == nil { return } startDeep = startDeep + 1 if node.Left == nil && node.Right == nil { if startDeep > max { max = startDeep } } dfs(node.Left, startDeep) dfs(node.Right, startDeep) } dfs(root, startDeep) return max}