The number of nodes in the tree is in the range [0, 5000].
-104 <= Node.val <= 104
解題
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */funcisBalanced(root *TreeNode) bool {if root ==nil { returntrue } left :=helper(root.Left) right :=helper(root.Right)returnabs(left-right) <=1&&isBalanced(root.Left) &&isBalanced(root.Right)}funchelper(root *TreeNode) int {if root ==nil { return0 }returnmax(helper(root.Left), helper(root.Right)) +1}funcmax(a, b int) int {if a > b { return a }return b}funcabs(n int) int {if n <0 { return-n }return n}