From 05e108ed96ea39ab1a3fea44ccb9404fff81c3b9 Mon Sep 17 00:00:00 2001 From: jeehay Date: Tue, 4 Mar 2025 22:12:31 +0900 Subject: [PATCH 1/2] Add lowest-common-ancestor-of-a-binary-search-tree solution --- .../Jeehay28.js | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 lowest-common-ancestor-of-a-binary-search-tree/Jeehay28.js diff --git a/lowest-common-ancestor-of-a-binary-search-tree/Jeehay28.js b/lowest-common-ancestor-of-a-binary-search-tree/Jeehay28.js new file mode 100644 index 000000000..f6f2ec3f2 --- /dev/null +++ b/lowest-common-ancestor-of-a-binary-search-tree/Jeehay28.js @@ -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; +// }; + From 7268f4053eb0ccb7970dc49f18aad1b577924670 Mon Sep 17 00:00:00 2001 From: jeehay Date: Sat, 8 Mar 2025 20:09:09 +0900 Subject: [PATCH 2/2] Add kth-smallest-element-in-a-bst solution --- kth-smallest-element-in-a-bst/Jeehay28.js | 82 +++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 kth-smallest-element-in-a-bst/Jeehay28.js diff --git a/kth-smallest-element-in-a-bst/Jeehay28.js b/kth-smallest-element-in-a-bst/Jeehay28.js new file mode 100644 index 000000000..37b317322 --- /dev/null +++ b/kth-smallest-element-in-a-bst/Jeehay28.js @@ -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]; +// };