-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path897-increasing-order-search-tree.js
More file actions
205 lines (145 loc) · 6.18 KB
/
897-increasing-order-search-tree.js
File metadata and controls
205 lines (145 loc) · 6.18 KB
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
//Dec 27th algo with a new solution
// Dec 23rd algo w Senada
// //DEC 22nd algo with Diana
// //DEC 20th algo w/ Diana
function TreeNode(val, left, right) {
this.val = (val===undefined ? 0 : val)
this.left = (left===undefined ? null : left)
this.right = (right===undefined ? null : right)
}
var increasingBST = function(root) {
if (!root) {
return root;
}
const leftRoot = increasingBST(root.left);
root.left = null;
root.right = increasingBST(root.right);
if (!leftRoot) {
return root;
}
let lastLeftNode = leftRoot;
while (lastLeftNode.right) {
lastLeftNode = lastLeftNode.right;
}
lastLeftNode.right = root;
return leftRoot;
};
// var increasingBST = function(root) { //[5,1,7]
// let result = []
// function inOrder(node){
// if(!node)return;
// inOrder(node.left);
// result.push(node.val);
// inOrder(node.right)
// }
// inOrder(root);
// console.log(result)
// let tempNode = new TreeNode(0);
// let returnNode = tempNode;
// for(let i = 0; i < result.length; i++){
// tempNode.right = new TreeNode(result[i]);
// tempNode = tempNode.right
// }
// return returnNode.right
// }
/////////////////////////////////////
// var increasingBST = function(root) { //[5,1,7]
// if(!root) return null;
// let stack = [], newRoot = null, newTree = null;
// while(root){ // outer while loop
// // left
// //1st while loop
// while(root){ //root === [5, 1, 7]
// console.log("***** 1st while LOOP **** ")
// stack.push(root); //[5, 1, 7]
// //stack === [ [5, 1, 7] ]
// console.log("root.left :: BEFORE ::", root.left) //[1]
// root = root.left; // root = [1]
// console.log("stack :", stack) //[ [5, 1, 7] ]
// console.log("root :", root)
// console.log("newRoot :", newRoot)
// console.log("newTree :", newTree)
// console.log("******** END 1st while LOOP **** ")
// }
// // while(root){ //root === [1]
// // stack.push(root);
// // //stack === [ [5, 1, 7], [1] ]
// // console.log("root.left", root.left) //null
// // root = root.left; // root = null
// // console.log("stack", stack)
// // //[ [5, 1, 7], [1] ]
// // }
// //2nd while loop
// //pop
// while(stack.length > 0){ // [ [5, 1, 7], [1] ] 2 > 0
// console.log("***** 2nd while LOOP **** ")
// // read/visit
// let node = stack.pop(); //node === [1]
// if(!newRoot){ //if there's no newRoot //newRoot === null TRUTHY
// console.log("***** 2nd while loop IF ")
// newRoot = newTree = node; //[1]
// // newRoot = newTree //null
// // newTree = node //[1]
// // newRoot = node //[1]
// console.log("stack :", stack) //[ [5, 1, 7] ]
// console.log("root :", root)
// console.log("newRoot :", newRoot)
// console.log("newTree :", newTree)
// console.log("node : ", node)
// } else { //if there IS a newRoot
// console.log("***** 2nd while loop else ")
// newTree.right = node;
// newTree = newTree.right;
// console.log("stack :", stack) //[ [5, 1, 7] ]
// console.log("root :", root)
// console.log("newRoot :", newRoot)
// console.log("newTree :", newTree)
// console.log("node : ", node)
// }
// node.left = null; //[1].left = null
// // right
// if(node.right){ //node === [1] so it's FALSEY
// console.log("***** 2nd while loop 2nd IF ")
// root = node.right;
// console.log("stack :", stack) //[ [5, 1, 7] ]
// console.log("root :", root)
// console.log("newRoot :", newRoot)
// console.log("newTree :", newTree)
// console.log("node : ", node)
// break;
// }
// console.log(" **** END 2nd while LOOP **** ")
// }
// // //pop
// // while(stack.length > 0){ // [ [5, 1, 7] ] 1 > 0
// // // read/visit
// // let node = stack.pop(); //node === [5,1,7]
// // console.log("node:::: ", node)
// // if(!newRoot){ //skip bc we have newRoot === [1]
// // newRoot = newTree = node; //skip
// // } else { //if there IS a newRoot YES
// //newTree = [1]
// // newTree.right = node; // newTree.right = [5,1,7]
// // newTree === [1, null, [5,1,7]]
// // newTree = newTree.right; newTree = [5,1,7]
// // newTree === [[5,1,7], null, null]
// // }
// // node.left = null; //[5,1,7].left = null
// // [5,1,7] ==> [5, null, 7]
// // // right
// // if(node.right){ //node === [1] so it's FALSEY
// // root = node.right;
// // console.log("RIGHT root:::::::::::::::::: ", root )
// // console.log("NEW ROOT*****", newRoot)
// // break;
// // }
// // }
// }
// console.log("NEW ROOT*****", newRoot)
// return newRoot; //[1, null, 5, null, 7]
// }
// // [5, 1, 7] [val, left, right] node.left == [5, null, 7]
// // node.right == null [5, null, null]
// // [1] === [1, null, null]
// // [7] === [7, null, null]
console.log(increasingBST([5,1,7]))