-
Notifications
You must be signed in to change notification settings - Fork 1
/
binary-tree-level-order-traversal-ii.js
183 lines (167 loc) · 3.82 KB
/
binary-tree-level-order-traversal-ii.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
/**
* Source: https://leetcode.com/problems/binary-tree-level-order-traversal-ii/
* Tags: [Tree,Breadth-first Search]
* Level: Easy
* Title: Binary Tree Level Order Traversal II
* Auther: @imcoddy
* Content: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
*
*
* For example:
* Given binary tree {3,9,20,#,#,15,7},
*
* 3
* / \
* 9 20
* / \
* 15 7
*
*
*
* return its bottom-up level order traversal as:
*
* [
* [15,7],
* [9,20],
* [3]
* ]
*
*
*
* 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: 134ms
* Rank: S
*/
var levelOrderBottom = function(root) {
if (!root) {
return [];
}
var current_nodes = [root];
var result = [
[root.val]
];
var current_count = 1;
while (current_nodes.length > 0) {
var next_nodes = [];
var node;
for (var i = 0; i < current_count; i++) {
node = current_nodes.shift();
if (node.left) next_nodes.push(node.left);
if (node.right) next_nodes.push(node.right);
}
current_nodes = next_nodes;
current_count = next_nodes.length;
if (current_count) {
var tmp = [];
for (var i = 0; i < current_count; i++) {
tmp.push(current_nodes[i].val);
}
result.unshift(tmp);
}
}
return result;
};
/**
* Memo: Level traversal
* Complex: O(n)
* Runtime: 140ms
* Tests: 34 test cases passed
* Rank: A
*/
var levelOrderBottom = function(root) {
if (!root) return [];
var nodes = [root]; // queue of nodes to traverse
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.unshift(array);
count = new_count;
}
return result;
};
/**
* Memo: Inorder traversal and push nodes value to that level accordingly.
* Complex: O(n)
* Runtime: 140ms
* Tests: 34 test cases passed
* Rank: S
* Updated: 2015-10-05
*/
function levelOrderBottom(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.reverse();
}
function TreeNode(val) {
this.val = val;
this.left = this.right = null;
}
var util = require("./util.js");
var should = require('should');
console.time('Runtime');
levelOrderBottom(util.arrayToTree([1])).should.eql([
[1]
]);
levelOrderBottom(util.arrayToTree([1, 2, 3, 4])).should.eql([
[4],
[2, 3],
[1]
]);
levelOrderBottom(util.arrayToTree([3, 9, 20, '#', '#', 15, 7])).should.eql([
[15, 7],
[9, 20],
[3]
])
console.timeEnd('Runtime');