-
Notifications
You must be signed in to change notification settings - Fork 0
/
110.go
56 lines (48 loc) · 924 Bytes
/
110.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package p110
/**
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree
in which the depth of the two subtrees of every node never differ by more than 1.
*/
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func heightAndBalance(node *TreeNode) (int, bool) {
if node == nil {
return 0, true
}
lh, lb := heightAndBalance(node.Left)
if lb {
rh, rb := heightAndBalance(node.Right)
if rb && abs(lh-rh) <= 1 {
return max(lh, rh) + 1, true
}
}
return 0, false
}
func isBalanced(root *TreeNode) bool {
_, b := heightAndBalance(root)
return b
}