Skip to content

Commit 8638e49

Browse files
committed
finish 102,103
1 parent 45200a0 commit 8638e49

File tree

2 files changed

+195
-0
lines changed

2 files changed

+195
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# 102. Binary Tree Level Order Traversal
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Tree, Breadth-first Search.
5+
- Similar Questions: Binary Tree Zigzag Level Order Traversal, Binary Tree Level Order Traversal II, Minimum Depth of Binary Tree, Binary Tree Vertical Order Traversal, Average of Levels in Binary Tree.
6+
7+
## Problem
8+
9+
Given a binary tree, return the *level order* traversal of its nodes' values. (ie, from left to right, level by level).
10+
11+
For example:
12+
Given binary tree ```[3,9,20,null,null,15,7]```,
13+
```
14+
3
15+
/ \
16+
9 20
17+
/ \
18+
15 7
19+
```
20+
21+
22+
return its level order traversal as:
23+
```
24+
[
25+
[3],
26+
[9,20],
27+
[15,7]
28+
]
29+
```
30+
31+
## Solution 1
32+
33+
```javascript
34+
/**
35+
* Definition for a binary tree node.
36+
* function TreeNode(val) {
37+
* this.val = val;
38+
* this.left = this.right = null;
39+
* }
40+
*/
41+
/**
42+
* @param {TreeNode} root
43+
* @return {number[][]}
44+
*/
45+
var levelOrder = function(root) {
46+
if (!root) return [];
47+
return helper([[root]], 0);
48+
};
49+
50+
var helper = function (res, level) {
51+
var now = res[level];
52+
var next = [];
53+
54+
for (var i = 0; i < now.length; i++) {
55+
if (now[i].left) next.push(now[i].left);
56+
if (now[i].right) next.push(now[i].right);
57+
now[i] = now[i].val;
58+
}
59+
60+
if (next.length) {
61+
res.push(next);
62+
helper(res, level + 1);
63+
}
64+
65+
return res;
66+
};
67+
```
68+
69+
**Explain:**
70+
71+
bfs
72+
73+
**Complexity:**
74+
75+
* Time complexity : O(n).
76+
* Space complexity : O(n).
77+
78+
## Solution 2
79+
80+
```javascript
81+
/**
82+
* Definition for a binary tree node.
83+
* function TreeNode(val) {
84+
* this.val = val;
85+
* this.left = this.right = null;
86+
* }
87+
*/
88+
/**
89+
* @param {TreeNode} root
90+
* @return {number[][]}
91+
*/
92+
var levelOrder = function(root) {
93+
return helper([], root, 0);
94+
};
95+
96+
var helper = function (res, root, level) {
97+
if (root) {
98+
if (!res[level]) res[level] = [];
99+
res[level].push(root.val);
100+
helper(res, root.left, level + 1);
101+
helper(res, root.right, level + 1);
102+
}
103+
return res;
104+
};
105+
```
106+
107+
**Explain:**
108+
109+
dfs
110+
111+
**Complexity:**
112+
113+
* Time complexity : O(n).
114+
* Space complexity : O(n).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# 103. Binary Tree Zigzag Level Order Traversal
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Stack, Tree, Breadth-first Search.
5+
- Similar Questions: Binary Tree Level Order Traversal.
6+
7+
## Problem
8+
9+
Given a binary tree, return the *zigzag level order* traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).
10+
11+
For example:
12+
Given binary tree ```[3,9,20,null,null,15,7]```,
13+
```
14+
3
15+
/ \
16+
9 20
17+
/ \
18+
15 7
19+
```
20+
21+
return its zigzag level order traversal as:
22+
```
23+
[
24+
[3],
25+
[20,9],
26+
[15,7]
27+
]
28+
```
29+
30+
## Solution
31+
32+
```javascript
33+
/**
34+
* Definition for a binary tree node.
35+
* function TreeNode(val) {
36+
* this.val = val;
37+
* this.left = this.right = null;
38+
* }
39+
*/
40+
/**
41+
* @param {TreeNode} root
42+
* @return {number[][]}
43+
*/
44+
var zigzagLevelOrder = function(root) {
45+
if (!root) return [];
46+
return helper([[root]], 0);
47+
};
48+
49+
var helper = function (res, level) {
50+
var now = res[level];
51+
var next = [];
52+
53+
for (var i = now.length - 1; i >= 0; i--) {
54+
if (level % 2) {
55+
if (now[i].left) next.push(now[i].left);
56+
if (now[i].right) next.push(now[i].right);
57+
} else {
58+
if (now[i].right) next.push(now[i].right);
59+
if (now[i].left) next.push(now[i].left);
60+
}
61+
62+
now[i] = now[i].val;
63+
}
64+
65+
if (next.length) {
66+
res.push(next);
67+
helper(res, level + 1);
68+
}
69+
70+
return res;
71+
};
72+
```
73+
74+
**Explain:**
75+
76+
nope.
77+
78+
**Complexity:**
79+
80+
* Time complexity : O(n).
81+
* Space complexity : O(n).

0 commit comments

Comments
 (0)