diff --git a/binary-tree-level-order-traversal/byol-han.js b/binary-tree-level-order-traversal/byol-han.js new file mode 100644 index 000000000..816f2d1c6 --- /dev/null +++ b/binary-tree-level-order-traversal/byol-han.js @@ -0,0 +1,36 @@ +/** + * https://leetcode.com/problems/binary-tree-level-order-traversal/description/ + * 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 + * @return {number[][]} + */ +var levelOrder = function (root) { + const result = []; + if (!root) return result; + + const queue = [root]; + + while (queue.length > 0) { + const levelSize = queue.length; + const level = []; + + for (let i = 0; i < levelSize; i++) { + const node = queue.shift(); + level.push(node.val); + + if (node.left) queue.push(node.left); + if (node.right) queue.push(node.right); + } + + result.push(level); + } + + return result; +}; diff --git a/house-robber-ii/byol-han.js b/house-robber-ii/byol-han.js new file mode 100644 index 000000000..a294bf72c --- /dev/null +++ b/house-robber-ii/byol-han.js @@ -0,0 +1,29 @@ +/** + * https://leetcode.com/problems/house-robber-ii/submissions/1686583649/ + * @param {number[]} nums + * @return {number} + */ +var rob = function (nums) { + if (nums.length === 1) return nums[0]; + + // Helper to solve the linear house robber problem + function robLinear(houses) { + let prev1 = 0; // dp[i-1] + let prev2 = 0; // dp[i-2] + + for (let money of houses) { + let temp = prev1; + prev1 = Math.max(prev1, prev2 + money); + prev2 = temp; + } + + return prev1; + } + + // Case 1: Exclude last house + let money1 = robLinear(nums.slice(0, nums.length - 1)); + // Case 2: Exclude first house + let money2 = robLinear(nums.slice(1)); + + return Math.max(money1, money2); +}; diff --git a/subtree-of-another-tree/byol-han.js b/subtree-of-another-tree/byol-han.js new file mode 100644 index 000000000..720e37ffe --- /dev/null +++ b/subtree-of-another-tree/byol-han.js @@ -0,0 +1,40 @@ +/** + * https://leetcode.com/problems/subtree-of-another-tree/description/ + * 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 {TreeNode} subRoot + * @return {boolean} + */ +var isSubtree = function (root, subRoot) { + // If the main tree is empty, it can't contain subRoot + if (!root) return false; + + // If the trees rooted at current node are the same, return true + if (isSameTree(root, subRoot)) { + return true; + } + + // Otherwise, recursively check left and right subtrees + return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot); +}; + +function isSameTree(s, t) { + // If both nodes are null, trees are identical at this branch + if (!s && !t) return true; + + // If only one is null, trees are not identical + if (!s || !t) return false; + + // If current node values are different, trees are not identical + if (s.val !== t.val) return false; + + // Recursively check left and right children + return isSameTree(s.left, t.left) && isSameTree(s.right, t.right); +}