Skip to content

Commit 62b738c

Browse files
committedJun 28, 2023
✨ [230] my approach
1 parent 5b30a4e commit 62b738c

File tree

3 files changed

+119
-1
lines changed

3 files changed

+119
-1
lines changed
 

‎230/my_solution.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
class TreeNode {
2+
constructor(val, left, right) {
3+
this.val = (val === undefined ? 0 : val);
4+
this.left = (left === undefined ? null : left);
5+
this.right = (right === undefined ? null : right);
6+
}
7+
}
8+
9+
/**
10+
* @param {TreeNode} root
11+
* @param {number} k
12+
* @return {number}
13+
*/
14+
var kthSmallest = function (root, k) {
15+
let list = [], kValue = null, parents = [];
16+
17+
const inOrder = (node) => {
18+
console.log("parents")
19+
console.log(parents)
20+
if (node === null || k === 0) return;
21+
22+
if (node.left !== null) { // going left
23+
parents.push(node);
24+
inOrder(node.left);
25+
} else { // returning to right
26+
k--;
27+
kValue = node.val;
28+
list.push(node.val);
29+
30+
if (node.right === null && parents.length > 0) {
31+
console.log("r null")
32+
let parent = parents.pop();
33+
parent.left = null;
34+
inOrder(parent);
35+
} else {
36+
inOrder(node.right);
37+
}
38+
}
39+
}
40+
41+
inOrder(root);
42+
43+
return kValue;
44+
};
45+
46+
47+
let x =
48+
// [1]
49+
kthSmallest(
50+
new TreeNode(1),
51+
1
52+
)
53+
// // [5,3,6,2,4,null,null,1]
54+
// kthSmallest(
55+
// new TreeNode(
56+
// 5,
57+
// new TreeNode(3,
58+
// new TreeNode(2, new TreeNode(1)),
59+
// new TreeNode(4)),
60+
// new TreeNode(6)
61+
// ),
62+
// 3
63+
// )
64+
// kthSmallest(
65+
// new TreeNode(
66+
// 3,
67+
// new TreeNode(1, null, new TreeNode(2)),
68+
// new TreeNode(4)
69+
// ),
70+
// 3
71+
// )
72+
73+
console.log("x")
74+
console.log(x)

‎230/solution.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class TreeNode {
2+
constructor(val, left, right) {
3+
this.val = (val === undefined ? 0 : val);
4+
this.left = (left === undefined ? null : left);
5+
this.right = (right === undefined ? null : right);
6+
}
7+
}
8+
9+
/**
10+
* @param {TreeNode} root
11+
* @param {number} k
12+
* @return {number}
13+
*/
14+
var kthSmallest = function (root, k) {
15+
let kValue = null, parents = [];
16+
17+
const inOrder = (node) => {
18+
console.log("parents")
19+
console.log(parents)
20+
if (node === null || k === 0) return;
21+
22+
if (node.left !== null) { // going left
23+
parents.push(node);
24+
inOrder(node.left);
25+
} else { // returning to right
26+
k--;
27+
kValue = node.val;
28+
29+
if (node.right === null && parents.length > 0) {
30+
console.log("r null")
31+
let parent = parents.pop();
32+
parent.left = null;
33+
inOrder(parent);
34+
} else {
35+
inOrder(node.right);
36+
}
37+
}
38+
}
39+
40+
inOrder(root);
41+
42+
return kValue;
43+
};

‎README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
- [213. House Robber II](./213/)
6060
- [217. Contains Duplicate](./217/)
6161
- [226. Invert Binary Tree](./226/)
62+
- [230. Kth Smallest Element in a BST](./230/)
6263
- [235. Lowest Common Ancestor of a Binary Search Tree](./235/)
6364
- [238. Product of Array Except Self](./238/)
6465
- [242. Valid Anagram](./242/)
@@ -145,7 +146,7 @@ Batch create:
145146
NOTE: JS IS HERE
146147
-->
147148
```ssh
148-
chapter=235 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
149+
chapter=230 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
149150
```
150151
> then you can use `x` for quick debug.
151152

0 commit comments

Comments
 (0)
Please sign in to comment.