-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinary-tree-level-order-traversal.js
207 lines (187 loc) · 4.29 KB
/
binary-tree-level-order-traversal.js
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* Source: https://leetcode.com/problems/binary-tree-level-order-traversal/
* Tags: [Tree,Breadth-first Search]
* Level: Easy
* Title: Binary Tree Level Order Traversal
* Auther: @imcoddy
* Content: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
*
*
* For example:
* Given binary tree {3,9,20,#,#,15,7},
*
* 3
* / \
* 9 20
* / \
* 15 7
*
*
*
* return its level order traversal as:
*
* [
* [3],
* [9,20],
* [15,7]
* ]
*
*
*
* confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
*
* OJ's Binary Tree Serialization:
*
* The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
*
*
* Here's an example:
*
* 1
* / \
* 2 3
* /
* 4
* \
* 5
*
* The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".
*/
/**
* Definition for binary tree
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @returns {number[][]}
*/
/**
* Memo:
* Runtime: 146ms
* Rank: S
*/
var levelOrder = function(root) {
if (!root) {
return [];
}
var current_nodes = [root];
var result = [
[root.val]
];
var current_count = 1;
while (current_nodes.length > 0) {
var next_count = 0;
var next_nodes = [];
var node;
for (var i = 0; i < current_count; i++) {
node = current_nodes.shift();
if (node.left) {
next_count++;
next_nodes.push(node.left);
}
if (node.right) {
next_count++;
next_nodes.push(node.right);
}
}
current_count = next_count;
current_nodes = next_nodes;
if (current_nodes.length) {
var tmp = [];
for (var i = 0; i < current_nodes.length; i++) {
tmp.push(current_nodes[i].val);
}
result.push(tmp);
}
}
return result;
};
/**
* Memo:
* Complex: O(n)
* Runtime: 128ms
* Tests: 34 test cases passed
* Rank: A
* Updated: 2015-10-05
*/
var levelOrder = function(root) {
if (!root) return [];
var nodes = [root];
var result = [];
var count = 1;
while (nodes.length) {
var new_count = 0;
var array = [];
for (var i = 0; i < count; i++) {
var node = nodes.shift();
array.push(node.val);
if (node.left) {
new_count++;
nodes.push(node.left);
}
if (node.right) {
new_count++;
nodes.push(node.right);
}
}
result.push(array);
count = new_count;
}
return result;
};
/**
* Memo: Use inorder to traverse the root recursively, record current depth and push current node.val to result of that level accordingly.
* Complex: O(n)
* Runtime: 146ms
* Tests: 34 test cases passed
* Rank: A
*/
var levelOrder = function(root) {
var result = [];
var traverse = function(root, depth) {
if (!root) return;
if (!result[depth]) result[depth] = [];
result[depth].push(root.val);
traverse(root.left, depth + 1);
traverse(root.right, depth + 1);
};
traverse(root, 0);
return result;
};
/**
* Memo: Inorder traversal and push nodes value to that level accordingly.
* Complex: O(n)
* Runtime: 120ms
* Tests: 34 test cases passed
* Rank: S
* Updated: 2015-10-05
*/
function levelOrder(root) {
var result = [];
function traverse(root, depth, result) {
if (!root) return;
if (!result[depth]) result[depth] = [];
result[depth].push(root.val);
traverse(root.left, depth + 1, result);
traverse(root.right, depth + 1, result);
}
traverse(root, 0, result);
return result;
}
function TreeNode(val) {
this.val = val;
this.left = this.right = null;
}
var node = new TreeNode(1);
var root = node;
console.log(levelOrder(root));
node = new TreeNode(2);
root.right = node;
node = new TreeNode(3);
root.left = node;
node = new TreeNode(4);
root.left.left = node;
console.log(levelOrder(root));