-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path103-BinaryTreeZigzagLevelOrderTraversal.go
More file actions
126 lines (113 loc) · 3.1 KB
/
103-BinaryTreeZigzagLevelOrderTraversal.go
File metadata and controls
126 lines (113 loc) · 3.1 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
// 103. Binary Tree Zigzag Level Order Traversal
// Given the root of a binary tree, return the zigzag level order traversal of its nodes' values.
// (i.e., from left to right, then right to left for the next level and alternate between).
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" / >
// Input: root = [3,9,20,null,null,15,7]
// Output: [[3],[20,9],[15,7]]
// Example 2:
// Input: root = [1]
// Output: [[1]]
// Example 3:
// Input: root = []
// Output: []
// Constraints:
// The number of nodes in the tree is in the range [0, 2000].
// -100 <= Node.val <= 100
import "fmt"
// Definition for a binary tree node.
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
// 递归 dfs
func zigzagLevelOrder(root *TreeNode) [][]int {
var res [][]int
var search func(root *TreeNode, depth int, res *[][]int)
search = func (root *TreeNode, depth int, res *[][]int) {
if root == nil {
return
}
for len(*res) < depth+1 {
*res = append(*res, []int{})
}
// 每一层的顺序是相互反转的,即上一层是从左往右,下一层就是从右往左
if depth%2 == 0 {
(*res)[depth] = append((*res)[depth], root.Val)
} else {
(*res)[depth] = append([]int{root.Val}, (*res)[depth]...)
}
search(root.Left, depth+1, res)
search(root.Right, depth+1, res)
}
// 按层序从上到下遍历一颗树
search(root, 0, &res)
return res
}
// BFS
func zigzagLevelOrder1(root *TreeNode) [][]int {
res := [][]int{}
if root == nil {
return res
}
q := []*TreeNode{root}
size, i, j, lay, tmp, flag := 0, 0, 0, []int{}, []*TreeNode{}, false
for len(q) > 0 {
size = len(q)
tmp = []*TreeNode{}
lay = make([]int, size)
j = size - 1
for i = 0; i < size; i++ {
root = q[0]
q = q[1:]
if !flag {
lay[i] = root.Val
} else {
lay[j] = root.Val
j--
}
if root.Left != nil {
tmp = append(tmp, root.Left)
}
if root.Right != nil {
tmp = append(tmp, root.Right)
}
}
res = append(res, lay)
flag = !flag
q = tmp
}
return res
}
func main() {
tree1 := &TreeNode {
3,
&TreeNode{9, nil, nil},
&TreeNode {
20,
&TreeNode{15, nil, nil},
&TreeNode{7, nil, nil},
},
}
tree3 := &TreeNode {
1,
nil,
nil,
}
fmt.Println(zigzagLevelOrder(tree1)) // [[3],[20,9],[15,7]]
fmt.Println(zigzagLevelOrder(nil)) // []
fmt.Println(zigzagLevelOrder(tree3)) // [1]
fmt.Println(zigzagLevelOrder1(tree1)) // [[3],[20,9],[15,7]]
fmt.Println(zigzagLevelOrder1(nil)) // []
fmt.Println(zigzagLevelOrder1(tree3)) // [1]
}