Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 46734b7

Browse files
committed
commit solution 103
1 parent fa8da1e commit 46734b7

File tree

2 files changed

+109
-4
lines changed

2 files changed

+109
-4
lines changed

‎solution/100-199/0103.binary-tree-zigzag-level-order-traversal/README.md

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,70 @@
1-
# [100. xxx](https://leetcode-cn.com/problems/recover-binary-search-tree)
1+
# [103. 二叉树的锯齿形层次遍历](https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/description/)
22

33
### 题目描述
44

5+
<p>给定一个二叉树,返回其节点值的锯齿形层次遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。</p>
6+
7+
<p>例如:<br>
8+
给定二叉树&nbsp;<code>[3,9,20,null,null,15,7]</code>,</p>
9+
10+
<pre> 3
11+
/ \
12+
9 20
13+
/ \
14+
15 7
15+
</pre>
16+
17+
<p>返回锯齿形层次遍历如下:</p>
18+
19+
<pre>[
20+
[3],
21+
[20,9],
22+
[15,7]
23+
]
24+
</pre>
525

626
### 解题思路
727

28+
1. BFS
829

930
### 具体解法
1031

11-
<!-- tabs:start -->
1232

1333
#### **Golang**
1434
```go
35+
type TreeNode struct {
36+
Val int
37+
Left *TreeNode
38+
Right *TreeNode
39+
}
1540

41+
func zigzagLevelOrder(root *TreeNode) [][]int {
42+
res := [][]int{}
43+
if root == nil {
44+
return res
45+
}
46+
queue := []*TreeNode{root}
47+
for level := 0; len(queue) > 0; level++ {
48+
l := len(queue)
49+
list := []int{}
50+
for i := 0; i < l; i++ {
51+
node := queue[i]
52+
if level%2 == 0 {
53+
list = append(list, node.Val)
54+
} else {
55+
list = append([]int{node.Val}, list...)
56+
}
57+
if node.Left != nil {
58+
queue = append(queue, node.Left)
59+
}
60+
if node.Right != nil {
61+
queue = append(queue, node.Right)
62+
}
63+
}
64+
queue = queue[l:]
65+
res = append(res, list)
66+
}
67+
return res
68+
}
1669
```
1770

18-
<!-- tabs:end -->
19-
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=103 lang=golang
5+
*
6+
* [103] 二叉树的锯齿形层次遍历
7+
*/
8+
9+
// @lc code=start
10+
/**
11+
* Definition for a binary tree node.
12+
* type TreeNode struct {
13+
* Val int
14+
* Left *TreeNode
15+
* Right *TreeNode
16+
* }
17+
*/
18+
19+
type TreeNode struct {
20+
Val int
21+
Left *TreeNode
22+
Right *TreeNode
23+
}
24+
25+
func zigzagLevelOrder(root *TreeNode) [][]int {
26+
res := [][]int{}
27+
if root == nil {
28+
return res
29+
}
30+
queue := []*TreeNode{root}
31+
for level := 0; len(queue) > 0; level++ {
32+
l := len(queue)
33+
list := []int{}
34+
for i := 0; i < l; i++ {
35+
node := queue[i]
36+
if level%2 == 0 {
37+
list = append(list, node.Val)
38+
} else {
39+
list = append([]int{node.Val}, list...)
40+
}
41+
if node.Left != nil {
42+
queue = append(queue, node.Left)
43+
}
44+
if node.Right != nil {
45+
queue = append(queue, node.Right)
46+
}
47+
}
48+
queue = queue[l:]
49+
res = append(res, list)
50+
}
51+
return res
52+
}
53+
54+
// @lc code=end

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /