题解 | #牛群的最大高度#
牛群的最大高度
https://www.nowcoder.com/practice/f745023c5ac641c9914a59377dacdacf
package main //import "fmt" import . "nc_tools" /* * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型 */ func findMaxHeight(root *TreeNode) int { // write code here max := 0 var deep func(*TreeNode, int) int deep = func(root *TreeNode, max int) int { if root == nil { return max } if root.Val > max { max = root.Val } m1 := deep(root.Left, max) m2 := deep(root.Right, max) if m1 > max { max = m1 } if m2 > max { max = m2 } return max } max = deep(root, max) return max }
解题思路:
- 只要遍历整个树形结构,对比出最大的值。