-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (62 loc) · 1.12 KB
/
main.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
57
58
59
60
61
62
63
64
65
66
67
68
package main
import "fmt"
type Node struct {
Val int
Left *Node
Right *Node
Next *Node
}
// 使用队列层次优先遍历
func connect(root *Node) *Node {
if root == nil {
return nil
}
queue := []*Node{root}
for len(queue) > 0 {
length := len(queue)
for i := 0; i < length; i++ {
cur := queue[0]
if length - i - 1 > 0 {
cur.Next = queue[1]
} else {
cur.Next = nil
}
queue = queue[1:]
if cur.Left != nil {
queue = append(queue, cur.Left)
}
if cur.Right != nil {
queue = append(queue, cur.Right)
}
}
}
return root
}
// 不使用额外空间
func connect2(root *Node) *Node {
if root == nil {
return nil
}
pre := root
for pre.Left != nil {
tmp := pre
for tmp != nil {
tmp.Left.Next = tmp.Right
if tmp.Next != nil {
tmp.Right.Next = tmp.Next.Left
}
tmp = tmp.Next
}
pre = pre.Left
}
return root
}
func main() {
root := connect(&Node{
1,
&Node{2, &Node{4, nil, nil, nil}, &Node{5, nil, nil, nil}, nil},
&Node{3, &Node{6, nil, nil, nil}, &Node{7, nil, nil, nil}, nil},
nil,
})
fmt.Println(root.Val, root.Left, root.Right, root.Next)
}