Skip to content

[Jeehay28] WEEK 13 #1076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions kth-smallest-element-in-a-bst/Jeehay28.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// 🚀 Optimized Approach: Inorder Traversal (BST Property)
// ✅ Time Complexity: O(n), n represents the number of nodes
// ✅ Space Complexity: O(n), due to recursion stack (in the worst case, e.g., unbalanced tree)

/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} k
* @return {number}
*/
var kthSmallest = function (root, k) {
// Correct Order in BST Inorder Traversal: 🌿 (left) → 🌟 (current) → 🌳 (right)
// 🌿 Go Left (visit the smallest values first).
// 🌟 Visit Current Node (increment count++).
// 🌳 Go Right (visit larger values).

let count = 0;
let result = null;

const inorder = (node) => {
if (!node || result !== null) return; // If node is null or result is found, stop recursion

inorder(node.left);

count += 1; // Visit current node
if (count === k) {
result = node.val;
return;
}

inorder(node.right);
};

inorder(root);

return result;
};


// 😊 My initial approach
// 🐌This is not optimal for a BST, but it works.
// ✅ Time Complexity: O(n * logn), n represents the number of nodes
// ✅ Space Complexity: O(n)

/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} k
* @return {number}
*/
// var kthSmallest = function (root, k) {
// let arr = [];
// const dfs = (node) => {
// if (!node) return null;

// if (node.val !== undefined) {
// arr.push(node.val);
// dfs(node.left);
// dfs(node.right);
// }
// };

// dfs(root);

// arr.sort((a, b) => a - b);

// return arr[k - 1];
// };
67 changes: 67 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/Jeehay28.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 🚀 Iterative approach (No recursion)
// ✅ Time Complexity: O(h), where h is the height of the tree
// ✅ Space Complexity: O(1) (better space efficiency)

/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/

/**
* @param {TreeNode} root
* @param {TreeNode} p
* @param {TreeNode} q
* @return {TreeNode}
*/
var lowestCommonAncestor = function (root, p, q) {
let node = root;

while (node) {
if (p.val < node.val && q.val < node.val) {
node = node.left;
} else if (node.val < p.val && node.val < q.val) {
node = node.right;
} else {
return node;
}
}
};


// 🚀 Recursive approach
// ✅ Time Complexity: O(h), where h is the height of the tree
// ✅ Space Complexity: O(h) (due to recursion stack)
// 🔍 The function moves down the BST recursively:
// - If both p and q are smaller, search the left subtree
// - If both p and q are larger, search the right subtree
// - Otherwise, root is the LCA

/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/

/**
* @param {TreeNode} root
* @param {TreeNode} p
* @param {TreeNode} q
* @return {TreeNode}
*/
// var lowestCommonAncestor = function (root, p, q) {
// if (p.val < root.val && q.val < root.val) {
// return lowestCommonAncestor(root.left, p, q);
// }

// if (root.val < p.val && root.val < q.val) {
// return lowestCommonAncestor(root.right, p, q);
// }

// return root;
// };