Skip to content

Commit b7ddc22

Browse files
committed
Add verify-preorder-serialization-of-a-binary-tree based on stack
1 parent 097dc96 commit b7ddc22

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

‎algorithms/binarysearch/69.mySqrt.go

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ func mySqrt(x int) int {
88
// 每次mid取值为(l + r + 1) / 2
99
// 这是为了防止当l = r - 1时,出现死循环的情况
1010
mid := (left + right + 1) >> 1
11-
// fmt.Printf("l:%d r:%d mid:%d\n", left, right, mid)
1211
if mid*mid <= x {
1312
left = mid
1413
} else {

‎algorithms/tree/331.isValidSerialization.go

+37-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package tree
22

3-
import "strings"
3+
import (
4+
"strings"
45

5-
func isValidSerialization(preorder string) bool {
6+
"Leetcode/algorithms/kit"
7+
)
8+
9+
// 读取到的结构只要符合二叉树性质而且不会在未读完之前就满足leaves = nodes + 1(完整的二叉树)即可
10+
func isValidSerialization1(preorder string) bool {
611
var leaves, node int
712
pres := strings.Split(preorder, ",")
813
for i, s := range pres {
@@ -22,5 +27,35 @@ func isValidSerialization(preorder string) bool {
2227
if leaves == node+1 {
2328
return true
2429
}
30+
2531
return false
2632
}
33+
34+
// 基于stack
35+
// 使用栈来模拟二叉树的遍历,就是如果此时栈中的前两层都是null("#"),那就将这颗子树弹出去,
36+
// 过程中要判断这棵子树的根节点是否为空,为空直接返回false,如果不为空将这棵树弹出去之后用一个"#"代替,
37+
// 这样一遍遍历下来栈中保留的就应该只有一个"#",对其进行判断,满足返回true,否则返回false
38+
func isValidSerialization2(preorder string) bool {
39+
if len(preorder) == 0 {
40+
return false
41+
}
42+
43+
array := strings.Split(preorder, ",")
44+
stack := kit.NewStack()
45+
for _, ele := range array {
46+
stack.Push(ele)
47+
for stack.Len() >= 3 && stack.Peek().(string) == "#" && stack.Seek(stack.Len()-2).(string) == "#" {
48+
stack.Pop()
49+
stack.Pop()
50+
// 如果节点为空,返回false
51+
if stack.Peek().(string) == "#" {
52+
return false
53+
}
54+
stack.Pop()
55+
56+
stack.Push("#")
57+
}
58+
}
59+
60+
return stack.Len() == 1 && stack.Peek().(string) == "#"
61+
}

‎algorithms/tree/331.isValidSerialization_test.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@ func Test_isValidSerialization(t *testing.T) {
2424
Input: "9,#,#,1",
2525
Expected: false,
2626
},
27-
// more test case
27+
// add more test cases
2828
}
2929

3030
for _, tt := range cases {
3131
t.Run(tt.Name, func(t *testing.T) {
32-
if output := isValidSerialization(tt.Input.(string)); output != tt.Expected.(bool) {
33-
t.Errorf("isValidSerialization(%s)=%t, expected=%t", tt.Input, output, tt.Expected.(bool))
32+
if output := isValidSerialization1(tt.Input.(string)); output != tt.Expected.(bool) {
33+
t.Errorf("isValidSerialization1(%s)=%t, expected=%t", tt.Input, output, tt.Expected.(bool))
34+
}
35+
36+
if output := isValidSerialization2(tt.Input.(string)); output != tt.Expected.(bool) {
37+
t.Errorf("isValidSerialization2(%s)=%t, expected=%t", tt.Input, output, tt.Expected.(bool))
3438
}
3539
})
3640
}

0 commit comments

Comments
 (0)