Skip to content

Commit 433ca7a

Browse files
authored
Merge pull request #1111 from Jeehay28/main
[Jeehay28] WEEK 15
2 parents 6db9bab + c39ca6d commit 433ca7a

File tree

3 files changed

+181
-0
lines changed

3 files changed

+181
-0
lines changed
+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// ✅ Time Complexity: O(n^2), where n represents the length of the input string s
2+
// ✅ Space Complexity: O(n)
3+
4+
/**
5+
* @param {string} s
6+
* @return {string}
7+
*/
8+
var longestPalindrome = function (s) {
9+
let max_left = 0,
10+
max_right = 0;
11+
12+
for (let i = 0; i < s.length; i++) {
13+
// Odd-length palindromes
14+
let left = i,
15+
right = i;
16+
17+
while (left >= 0 && right < s.length && s[left] === s[right]) {
18+
if (max_right - max_left < right - left) {
19+
max_right = right;
20+
max_left = left;
21+
}
22+
left -= 1;
23+
right += 1;
24+
}
25+
26+
// Even-length palindromes
27+
left = i;
28+
right = i + 1;
29+
30+
while (left >= 0 && right < s.length && s[left] === s[right]) {
31+
if (max_right - max_left < right - left) {
32+
max_right = right;
33+
max_left = left;
34+
}
35+
left -= 1;
36+
right += 1;
37+
}
38+
}
39+
40+
return s.slice(max_left, max_right + 1);
41+
};
42+
43+
// ✅ Time Complexity: O(n^3), where n represents the length of the input string s
44+
// ✅ Space Complexity: O(n)
45+
46+
/**
47+
* @param {string} s
48+
* @return {string}
49+
*/
50+
// var longestPalindrome = function (s) {
51+
// const isPalindromic = (left, right) => {
52+
// while (left < right) {
53+
// if (s[left] !== s[right]) {
54+
// return false;
55+
// }
56+
// left += 1;
57+
// right -= 1;
58+
// }
59+
60+
// return true;
61+
// };
62+
63+
// let max_left = 0,
64+
// max_right = 0;
65+
// for (let l = 0; l < s.length; l++) {
66+
// for (let r = 0; r < s.length; r++) {
67+
// if (isPalindromic(l, r) && max_right - max_left < r - l) {
68+
// max_left = l;
69+
// max_right = r;
70+
// }
71+
// }
72+
// }
73+
74+
// return s.slice(max_left, max_right + 1);
75+
// };
76+

subtree-of-another-tree/Jeehay28.js

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// ✅ Time Complexity: O(m * n) (due to the substring search), where m is the number of nodes in root and n is the number of nodes in subRoot.
2+
// ✅ Space Complexity: O(m + n)
3+
4+
/**
5+
* Definition for a binary tree node.
6+
* function TreeNode(val, left, right) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.left = (left===undefined ? null : left)
9+
* this.right = (right===undefined ? null : right)
10+
* }
11+
*/
12+
/**
13+
* @param {TreeNode} root
14+
* @param {TreeNode} subRoot
15+
* @return {boolean}
16+
*/
17+
var isSubtree = function (root, subRoot) {
18+
// Helper function: Serializes a tree into a string representation in a pre-order fashion
19+
// by visiting the node first, then left and right subtrees. This ensures each node is uniquely
20+
// represented with its value, and the structure is captured recursively.
21+
const serialize = (node) => {
22+
if (!node) return "#"; // Use "#" to represent null nodes, ensuring we capture structure.
23+
return `(${node.val},${serialize(node.left)},${serialize(node.right)})`; // Recursively serialize left and right children.
24+
};
25+
26+
const serializedRoot = serialize(root); // O(m) for serializing root.
27+
const serializedSubRoot = serialize(subRoot); // O(n) for serializing subRoot.
28+
29+
return serializedRoot.includes(serializedSubRoot); // O(m * n) for the substring search.
30+
};
31+
32+
33+
34+
// ✅ Time Complexity: O(m * n), where m is the number of nodes in root and n is the number of nodes in subRoot.
35+
// ✅ Space Complexity: O(m + n), due to the recursion stack for both isSubtree and isSameTree.
36+
37+
/**
38+
* Definition for a binary tree node.
39+
* function TreeNode(val, left, right) {
40+
* this.val = (val===undefined ? 0 : val)
41+
* this.left = (left===undefined ? null : left)
42+
* this.right = (right===undefined ? null : right)
43+
* }
44+
*/
45+
/**
46+
* @param {TreeNode} root
47+
* @param {TreeNode} subRoot
48+
* @return {boolean}
49+
*/
50+
// var isSubtree = function (root, subRoot) {
51+
// // Base case: if root is null, subRoot can't be a subtree
52+
// if (!root) return false;
53+
54+
// // Helper function to check if two trees are identical
55+
// const isSameTree = (node1, node2) => {
56+
// if (!node1 && !node2) return true; // Both are null, they are identical
57+
// if (!node1 || !node2) return false; // One is null, other is not, they are not identical
58+
59+
// if (node1.val !== node2.val) return false; // Values don't match, not identical
60+
61+
// // Recursively check both left and right subtrees
62+
// return (
63+
// isSameTree(node1.left, node2.left) && isSameTree(node1.right, node2.right)
64+
// );
65+
// };
66+
67+
// // If the current node of root is identical to subRoot, return true
68+
// if (isSameTree(root, subRoot)) return true;
69+
70+
// // isSubtree is true if subRoot exists as a subtree in either the left or right side of root.
71+
// // It checks both sides recursively until it finds a match, and if found, returns true.
72+
// // If it doesn't find a match in either subtree, it will return false.
73+
// return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
74+
// };
75+
76+
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// ✅ Time Complexity: O(N), where N is the number of nodes in the tree.
2+
// ✅ Space Complexity: O(N)
3+
4+
/**
5+
* Definition for a binary tree node.
6+
* function TreeNode(val, left, right) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.left = (left===undefined ? null : left)
9+
* this.right = (right===undefined ? null : right)
10+
* }
11+
*/
12+
/**
13+
* @param {TreeNode} root
14+
* @return {boolean}
15+
*/
16+
var isValidBST = function (root) {
17+
// Helper function to check BST validity
18+
const dfs = (node, low, high) => {
19+
// Base case: Empty subtree is valid
20+
if (!node) return true;
21+
22+
if (!(low < node.val && node.val < high)) return false;
23+
24+
return dfs(node.left, low, node.val) && dfs(node.right, node.val, high);
25+
};
26+
27+
return dfs(root, -Infinity, Infinity);
28+
};
29+

0 commit comments

Comments
 (0)