Skip to content

[byol-han] WEEK 15 solutions #1656

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
36 changes: 36 additions & 0 deletions binary-tree-level-order-traversal/byol-han.js
Original file line number Diff line number Diff line change
@@ -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;
};
29 changes: 29 additions & 0 deletions house-robber-ii/byol-han.js
Original file line number Diff line number Diff line change
@@ -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);
};
40 changes: 40 additions & 0 deletions subtree-of-another-tree/byol-han.js
Original file line number Diff line number Diff line change
@@ -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);
}