题目:
题解:
Go
func findTilt(root *TreeNode) (ans int) {
var dfs func(*TreeNode) int
dfs = func(node *TreeNode) int {
if node == nil {
return 0
}
sumLeft := dfs(node.Left)
sumRight := dfs(node.Right)
ans += abs(sumLeft - sumRight)
return sumLeft + sumRight + node.Val
}
dfs(root)
return
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}