二叉树递归
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
var sum = 0
func findTilt(root *TreeNode) int {
helper(root)
return sum
}
func helper(root *TreeNode) int {
if root == nil {
return 0
}
left := helper(root.Left)
right := helper(root.Right)
sum += int(math.Abs(float64(left) - float64(right)))
return root.Val + left + right
}
更多内容请移步我的repo:https://github.com/anakin/golang-leetcode