Skip to content

Commit 40f6718

Browse files
authored
Merge pull request #1076 from Jeehay28/main
[Jeehay28] WEEK 13
2 parents 897bea0 + 7268f40 commit 40f6718

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed
+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// 🚀 Optimized Approach: Inorder Traversal (BST Property)
2+
// ✅ Time Complexity: O(n), n represents the number of nodes
3+
// ✅ Space Complexity: O(n), due to recursion stack (in the worst case, e.g., unbalanced tree)
4+
5+
/**
6+
* Definition for a binary tree node.
7+
* function TreeNode(val, left, right) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
*/
13+
/**
14+
* @param {TreeNode} root
15+
* @param {number} k
16+
* @return {number}
17+
*/
18+
var kthSmallest = function (root, k) {
19+
// Correct Order in BST Inorder Traversal: 🌿 (left) → 🌟 (current) → 🌳 (right)
20+
// 🌿 Go Left (visit the smallest values first).
21+
// 🌟 Visit Current Node (increment count++).
22+
// 🌳 Go Right (visit larger values).
23+
24+
let count = 0;
25+
let result = null;
26+
27+
const inorder = (node) => {
28+
if (!node || result !== null) return; // If node is null or result is found, stop recursion
29+
30+
inorder(node.left);
31+
32+
count += 1; // Visit current node
33+
if (count === k) {
34+
result = node.val;
35+
return;
36+
}
37+
38+
inorder(node.right);
39+
};
40+
41+
inorder(root);
42+
43+
return result;
44+
};
45+
46+
47+
// 😊 My initial approach
48+
// 🐌This is not optimal for a BST, but it works.
49+
// ✅ Time Complexity: O(n * logn), n represents the number of nodes
50+
// ✅ Space Complexity: O(n)
51+
52+
/**
53+
* Definition for a binary tree node.
54+
* function TreeNode(val, left, right) {
55+
* this.val = (val===undefined ? 0 : val)
56+
* this.left = (left===undefined ? null : left)
57+
* this.right = (right===undefined ? null : right)
58+
* }
59+
*/
60+
/**
61+
* @param {TreeNode} root
62+
* @param {number} k
63+
* @return {number}
64+
*/
65+
// var kthSmallest = function (root, k) {
66+
// let arr = [];
67+
// const dfs = (node) => {
68+
// if (!node) return null;
69+
70+
// if (node.val !== undefined) {
71+
// arr.push(node.val);
72+
// dfs(node.left);
73+
// dfs(node.right);
74+
// }
75+
// };
76+
77+
// dfs(root);
78+
79+
// arr.sort((a, b) => a - b);
80+
81+
// return arr[k - 1];
82+
// };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// 🚀 Iterative approach (No recursion)
2+
// ✅ Time Complexity: O(h), where h is the height of the tree
3+
// ✅ Space Complexity: O(1) (better space efficiency)
4+
5+
/**
6+
* Definition for a binary tree node.
7+
* function TreeNode(val) {
8+
* this.val = val;
9+
* this.left = this.right = null;
10+
* }
11+
*/
12+
13+
/**
14+
* @param {TreeNode} root
15+
* @param {TreeNode} p
16+
* @param {TreeNode} q
17+
* @return {TreeNode}
18+
*/
19+
var lowestCommonAncestor = function (root, p, q) {
20+
let node = root;
21+
22+
while (node) {
23+
if (p.val < node.val && q.val < node.val) {
24+
node = node.left;
25+
} else if (node.val < p.val && node.val < q.val) {
26+
node = node.right;
27+
} else {
28+
return node;
29+
}
30+
}
31+
};
32+
33+
34+
// 🚀 Recursive approach
35+
// ✅ Time Complexity: O(h), where h is the height of the tree
36+
// ✅ Space Complexity: O(h) (due to recursion stack)
37+
// 🔍 The function moves down the BST recursively:
38+
// - If both p and q are smaller, search the left subtree
39+
// - If both p and q are larger, search the right subtree
40+
// - Otherwise, root is the LCA
41+
42+
/**
43+
* Definition for a binary tree node.
44+
* function TreeNode(val) {
45+
* this.val = val;
46+
* this.left = this.right = null;
47+
* }
48+
*/
49+
50+
/**
51+
* @param {TreeNode} root
52+
* @param {TreeNode} p
53+
* @param {TreeNode} q
54+
* @return {TreeNode}
55+
*/
56+
// var lowestCommonAncestor = function (root, p, q) {
57+
// if (p.val < root.val && q.val < root.val) {
58+
// return lowestCommonAncestor(root.left, p, q);
59+
// }
60+
61+
// if (root.val < p.val && root.val < q.val) {
62+
// return lowestCommonAncestor(root.right, p, q);
63+
// }
64+
65+
// return root;
66+
// };
67+

0 commit comments

Comments
 (0)