-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflatten-binary-tree-to-linked-list.js
213 lines (190 loc) · 4.65 KB
/
flatten-binary-tree-to-linked-list.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
208
209
210
211
212
213
/**
* Source: https://leetcode.com/problems/flatten-binary-tree-to-linked-list/
* Tags: [Tree,Depth-first Search]
* Level: Medium
* Title: Flatten Binary Tree to Linked List
* Auther: @imcoddy
* Content: Given a binary tree, flatten it to a linked list in-place.
*
*
*
* For example,
* Given
*
* 1
* / \
* 2 5
* / \ \
* 3 4 6
*
*
*
* The flattened tree should look like:
*
* 1
* \
* 2
* \
* 3
* \
* 4
* \
* 5
* \
* 6
*
*
* click to show hints.
*
* Hints:
* If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal.
*/
/**
* Definition for binary tree
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @returns {void} Do not return anything, modify nodes in-place instead.
*/
/**
* Memo: Depth-first Search and put node in a queue, then change all child nodes to root's right child
* Runtime: 174ms
* Rank: S
*/
var flatten = function(root) {
function DFS(root, queue) {
if (!root) {
return [];
}
queue.push(root);
DFS(root.left, queue);
DFS(root.right, queue);
return queue;
}
var queue = DFS(root, []);
var p = queue.shift();
while (queue.length > 0) {
p.left = null;
p.right = queue.shift();
p = p.right;
}
};
/**
* Memo: Use a queue to save traversal order, and then adjust left child to right of each node
* Complex: O(n)
* Runtime: 168ms
* Tests: 225 test cases passed
* Rank: B
*/
var flatten = function(root) {
var queue = [];
function traverse(root) {
if (root) {
queue.push(root);
traverse(root.left);
traverse(root.right);
}
}
traverse(root);
var p = queue.shift();
while (queue.length > 0) {
p.left = null;
p.right = queue.shift();
p = p.right;
}
};
/**
* Memo: Recusive solution, use flatternTree to return last node of its subtree, then adjust root to combine flatterned left and right, return last node for further operation.
* Complex: O(n)
* Runtime: 156ms
* Tests: 225 test cases passed
* Rank: B
*/
var flatten = function(root) {
// flatten a tree and return its last node
function flatternTree(root) {
if (!root || (!root.left && !root.right)) {
return root;
}
var leftTail = flatternTree(root.left);
var rightTail = flatternTree(root.right);
if (!leftTail) {
return rightTail ? rightTail : root;
}
leftTail.right = root.right;
root.right = root.left;
root.left = null;
return rightTail ? rightTail : leftTail;
}
flatternTree(root);
};
/**
* Memo: Non recusive solution. Since this is a inorder traversal, root.right will end up to root.left's farest right child, so move root right there first, move left to right, then can search root.right using the same methodology
* Complex: O(n)
* Runtime: 148ms
* Tests: 225 test cases passed
* Rank: S
*/
var flatten = function(root) {
while (root) {
if (root.left) {
var t = root.left;
while (t.right) {
t = t.right;
}
t.right = root.right;
root.right = root.left;
root.left = null;
}
root = root.right;
}
};
/**
* Memo: A bit better than the above solution, as if root.right is null, there's no need to move root.right instead of jumping to root.left directly, and if root.left is null, there's no need to set it to null again :)
* Ref: https://leetcode.com/discuss/36732/8ms-non-recursive-no-stack-c-solution
* Complex: O(n)
* Runtime: 148ms
* Tests: 225 test cases passed
* Rank: S
*/
var flatten = function(root) {
while (root) {
if (root.left && root.right) {
var t = root.left;
while (t.right) {
t = t.right;
}
t.right = root.right;
}
if (root.left) {
root.right = root.left;
root.left = null;
}
root = root.right;
}
};
function TreeNode(val) {
this.val = val;
this.left = this.right = null;
}
var node = new TreeNode(1);
var root = node;
node = new TreeNode(2);
root.left = node;
node = new TreeNode(3);
root.right = node;
node = new TreeNode(4);
root.right.left = node;
node = new TreeNode(5);
root.right.right = node;
flatten(root);
console.log(root);
var p = root;
while (p) {
console.log(p.val);
p = p.right;
}