Skip to content

Commit c1dd1a5

Browse files
committed
feat: add golang solution to lcci problems: No. 04.04, 04.05
1 parent 7094659 commit c1dd1a5

File tree

6 files changed

+225
-0
lines changed

6 files changed

+225
-0
lines changed

lcci/04.04.Check Balance/README.md

+36
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,42 @@ class Solution {
7272
}
7373
```
7474

75+
### **Go**
76+
77+
自底向上递归
78+
79+
```go
80+
func isBalanced(root *TreeNode) bool {
81+
return depth(root) >= 0
82+
}
83+
84+
func depth(root *TreeNode) int {
85+
if root == nil {
86+
return 0
87+
}
88+
left := depth(root.Left)
89+
right := depth(root.Right)
90+
if left == -1 || right == -1 || abs(left-right) > 1 {
91+
return -1
92+
}
93+
return max(left, right) + 1
94+
}
95+
96+
func max(x, y int) int {
97+
if x > y {
98+
return x
99+
}
100+
return y
101+
}
102+
103+
func abs(x int) int {
104+
if x < 0 {
105+
return -x
106+
}
107+
return x
108+
}
109+
```
110+
75111
### **...**
76112

77113
```

lcci/04.04.Check Balance/README_EN.md

+36
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,42 @@ class Solution {
107107
}
108108
```
109109

110+
### **Go**
111+
112+
Bottom-up recursion
113+
114+
```go
115+
func isBalanced(root *TreeNode) bool {
116+
return depth(root) >= 0
117+
}
118+
119+
func depth(root *TreeNode) int {
120+
if root == nil {
121+
return 0
122+
}
123+
left := depth(root.Left)
124+
right := depth(root.Right)
125+
if left == -1 || right == -1 || abs(left-right) > 1 {
126+
return -1
127+
}
128+
return max(left, right) + 1
129+
}
130+
131+
func max(x, y int) int {
132+
if x > y {
133+
return x
134+
}
135+
return y
136+
}
137+
138+
func abs(x int) int {
139+
if x < 0 {
140+
return -x
141+
}
142+
return x
143+
}
144+
```
145+
110146
### **...**
111147

112148
```

lcci/04.04.Check Balance/Solution.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
func isBalanced(root *TreeNode) bool {
2+
return depth(root) >= 0
3+
}
4+
5+
func depth(root *TreeNode) int {
6+
if root == nil {
7+
return 0
8+
}
9+
left := depth(root.Left)
10+
right := depth(root.Right)
11+
if left == -1 || right == -1 || abs(left-right) > 1 {
12+
return -1
13+
}
14+
return max(left, right) + 1
15+
}
16+
17+
func max(x, y int) int {
18+
if x > y {
19+
return x
20+
}
21+
return y
22+
}
23+
24+
func abs(x int) int {
25+
if x < 0 {
26+
return -x
27+
}
28+
return x
29+
}

lcci/04.05.Legal Binary Search Tree/README.md

+45
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,51 @@ class Solution {
8383
}
8484
```
8585

86+
### **Go**
87+
88+
- 非递归中序遍历
89+
90+
```go
91+
func isValidBST(root *TreeNode) bool {
92+
stack := make([]*TreeNode, 0)
93+
var prev *TreeNode = nil
94+
node := root
95+
for len(stack) > 0 || node != nil {
96+
for node != nil {
97+
stack = append(stack, node)
98+
node = node.Left
99+
}
100+
node = stack[len(stack)-1]
101+
stack = stack[:len(stack)-1]
102+
if prev == nil || node.Val > prev.Val {
103+
prev = node
104+
} else {
105+
return false
106+
}
107+
node = node.Right
108+
}
109+
return true
110+
}
111+
```
112+
113+
- 利用上界下界判定
114+
115+
```go
116+
func isValidBST(root *TreeNode) bool {
117+
return check(root, math.MinInt64, math.MaxInt64)
118+
}
119+
120+
func check(node *TreeNode, lower, upper int) bool {
121+
if node == nil {
122+
return true
123+
}
124+
if node.Val <= lower || node.Val >= upper {
125+
return false
126+
}
127+
return check(node.Left, lower, node.Val) && check(node.Right, node.Val, upper)
128+
}
129+
```
130+
86131
### **...**
87132

88133
```

lcci/04.05.Legal Binary Search Tree/README_EN.md

+45
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,51 @@ class Solution {
112112
}
113113
```
114114

115+
### **Go**
116+
117+
- Non-recursive in-order traversal
118+
119+
```go
120+
func isValidBST(root *TreeNode) bool {
121+
stack := make([]*TreeNode, 0)
122+
var prev *TreeNode = nil
123+
node := root
124+
for len(stack) > 0 || node != nil {
125+
for node != nil {
126+
stack = append(stack, node)
127+
node = node.Left
128+
}
129+
node = stack[len(stack)-1]
130+
stack = stack[:len(stack)-1]
131+
if prev == nil || node.Val > prev.Val {
132+
prev = node
133+
} else {
134+
return false
135+
}
136+
node = node.Right
137+
}
138+
return true
139+
}
140+
```
141+
142+
- Use upper bound and lower bound to check
143+
144+
```go
145+
func isValidBST(root *TreeNode) bool {
146+
return check(root, math.MinInt64, math.MaxInt64)
147+
}
148+
149+
func check(node *TreeNode, lower, upper int) bool {
150+
if node == nil {
151+
return true
152+
}
153+
if node.Val <= lower || node.Val >= upper {
154+
return false
155+
}
156+
return check(node.Left, lower, node.Val) && check(node.Right, node.Val, upper)
157+
}
158+
```
159+
115160
### **...**
116161

117162
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
func isValidBST(root *TreeNode) bool {
2+
return check(root, math.MinInt64, math.MaxInt64)
3+
}
4+
5+
func check(node *TreeNode, lower, upper int) bool {
6+
if node == nil {
7+
return true
8+
}
9+
if node.Val <= lower || node.Val >= upper {
10+
return false
11+
}
12+
return check(node.Left, lower, node.Val) && check(node.Right, node.Val, upper)
13+
}
14+
15+
// func isValidBST(root *TreeNode) bool {
16+
// stack := make([]*TreeNode, 0)
17+
// var prev *TreeNode = nil
18+
// node := root
19+
// for len(stack) > 0 || node != nil {
20+
// for node != nil {
21+
// stack = append(stack, node)
22+
// node = node.Left
23+
// }
24+
// node = stack[len(stack)-1]
25+
// stack = stack[:len(stack)-1]
26+
// if prev == nil || node.Val > prev.Val {
27+
// prev = node
28+
// } else {
29+
// return false
30+
// }
31+
// node = node.Right
32+
// }
33+
// return true
34+
// }

0 commit comments

Comments
 (0)