Skip to content

Commit 13c30ad

Browse files
committed
♻️ [230]
1 parent 62b738c commit 13c30ad

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

230/my_solution.js

+32-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class TreeNode {
1212
* @return {number}
1313
*/
1414
var kthSmallest = function (root, k) {
15-
let list = [], kValue = null, parents = [];
15+
let list = [], kValue = null, parents = []; // stack
1616

1717
const inOrder = (node) => {
1818
console.log("parents")
@@ -42,6 +42,37 @@ var kthSmallest = function (root, k) {
4242

4343
return kValue;
4444
};
45+
// var kthSmallest = function (root, k) {
46+
// let list = [], kValue = null, parents = [];
47+
//
48+
// const inOrder = (node) => {
49+
// console.log("parents")
50+
// console.log(parents)
51+
// if (node === null || k === 0) return;
52+
//
53+
// if (node.left !== null) { // going left
54+
// parents.push(node);
55+
// inOrder(node.left);
56+
// } else { // returning to right
57+
// k--;
58+
// kValue = node.val;
59+
// list.push(node.val);
60+
//
61+
// if (node.right === null && parents.length > 0) {
62+
// console.log("r null")
63+
// let parent = parents.pop();
64+
// parent.left = null;
65+
// inOrder(parent);
66+
// } else { // going to right if there is a right node otherwise we go to the the last parent
67+
// inOrder(node.right);
68+
// }
69+
// }
70+
// }
71+
//
72+
// inOrder(root);
73+
//
74+
// return kValue;
75+
// };
4576

4677

4778
let x =

230/solution.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,16 @@ var kthSmallest = function (root, k) {
1515
let kValue = null, parents = [];
1616

1717
const inOrder = (node) => {
18-
console.log("parents")
19-
console.log(parents)
2018
if (node === null || k === 0) return;
2119

22-
if (node.left !== null) { // going left
20+
if (node.left !== null) {
2321
parents.push(node);
2422
inOrder(node.left);
25-
} else { // returning to right
23+
} else {
2624
k--;
2725
kValue = node.val;
2826

2927
if (node.right === null && parents.length > 0) {
30-
console.log("r null")
3128
let parent = parents.pop();
3229
parent.left = null;
3330
inOrder(parent);

0 commit comments

Comments
 (0)