Skip to content

Commit abd6d21

Browse files
committed
Add subtree of another tree LC medium JS
1 parent 2ac1c6f commit abd6d21

File tree

6 files changed

+131
-1
lines changed

6 files changed

+131
-1
lines changed

src/leetcode/easy/easy.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Table of Contents
44

5+
- [Balanced Binary Tree](balanced-binary-tree)
56
- [Best Time to Buy and Sell Stock](best-time-to-buy-and-sell-stock)
67
- [Binary Tree Diameter](binary-tree-diameter)
78
- [Binary Search](binary-search)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
https://leetcode.com/problems/subtree-of-another-tree
3+
4+
Given the roots of two binary trees root and subRoot, return true if there is a subtree of root with the same structure and node values of subRoot and false otherwise.
5+
6+
A subtree of a binary tree tree is a tree that consists of a node in tree and all of this node's descendants. The tree tree could also be considered as a subtree of itself.
7+
8+
Example 1:
9+
Input: root = [1,2,3,4,5], subRoot = [2,4,5]
10+
Output: true
11+
12+
Example 2:
13+
Input: root = [1,2,3,4,5,null,null,6], subRoot = [2,4,5]
14+
Output: false
15+
16+
Constraints:
17+
1 <= The number of nodes in both trees <= 100.
18+
-100 <= root.val, subRoot.val <= 100
19+
*/
20+
21+
class TreeNode {
22+
constructor(val = 0, left = null, right = null) {
23+
this.val = val;
24+
this.left = left;
25+
this.right = right;
26+
}
27+
}
28+
29+
const isSameTree = (node1, node2) => {
30+
if (!node1 && !node2) {
31+
return true;
32+
} else if (!node1 || !node2) {
33+
return false;
34+
}
35+
36+
const hasSameVal = node1.val === node2.val;
37+
if (!hasSameVal) {
38+
return false;
39+
}
40+
41+
const leftResult = isSameTree(node1.left, node2.left);
42+
const rightResult = isSameTree(node1.right, node2.right);
43+
44+
return leftResult && rightResult;
45+
};
46+
47+
const isSubtree = (root, subRoot) => {
48+
if (!root) {
49+
return !subRoot;
50+
}
51+
if (!subRoot) {
52+
return !root;
53+
}
54+
55+
const stack = [];
56+
if (root) {
57+
stack.push(root);
58+
}
59+
60+
while (stack.length > 0) {
61+
const currentNode = stack.pop();
62+
63+
if (currentNode.val === subRoot.val) {
64+
const result = isSameTree(currentNode, subRoot);
65+
66+
if (result) {
67+
return true;
68+
}
69+
}
70+
71+
if (currentNode.left) {
72+
stack.push(currentNode.left);
73+
}
74+
if (currentNode.right) {
75+
stack.push(currentNode.right);
76+
}
77+
}
78+
79+
return false;
80+
};
81+
82+
module.exports = { isSubtree, TreeNode };
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const { isSubtree, TreeNode } = require("./subtreeOfAnotherTree");
2+
3+
describe("isSubtree", () => {
4+
it("should return true if the subtree is found in the main tree", () => {
5+
const root = new TreeNode(1);
6+
root.left = new TreeNode(2);
7+
root.right = new TreeNode(3);
8+
root.left.left = new TreeNode(4);
9+
root.left.right = new TreeNode(5);
10+
const subRoot = new TreeNode(2);
11+
subRoot.left = new TreeNode(4);
12+
subRoot.right = new TreeNode(5);
13+
expect(isSubtree(root, subRoot)).toBe(true);
14+
});
15+
16+
it("should return false if the subtree is not found in the main tree", () => {
17+
const root = new TreeNode(1);
18+
root.left = new TreeNode(2);
19+
root.right = new TreeNode(3);
20+
root.left.left = new TreeNode(4);
21+
root.left.right = new TreeNode(5);
22+
const subRoot = new TreeNode(2);
23+
subRoot.left = new TreeNode(4);
24+
subRoot.right = new TreeNode(6);
25+
expect(isSubtree(root, subRoot)).toBe(false);
26+
});
27+
28+
it("should return false if the root tree is present but the subtree is not", () => {
29+
const root = new TreeNode(1);
30+
root.left = new TreeNode(2);
31+
root.right = new TreeNode(3);
32+
root.left.left = new TreeNode(4);
33+
root.left.right = new TreeNode(5);
34+
expect(isSubtree(root, undefined)).toBe(false);
35+
});
36+
37+
it("should return false if the subtree is present but the root tree is not", () => {
38+
const subRoot = new TreeNode(1);
39+
subRoot.left = new TreeNode(2);
40+
subRoot.right = new TreeNode(3);
41+
expect(isSubtree(undefined, subRoot)).toBe(false);
42+
});
43+
44+
it("should return true if both the root and subtree are empty", () => {
45+
expect(isSubtree(undefined, undefined)).toBe(true);
46+
});
47+
});

src/leetcode/medium/medium.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
- [Add Two Numbers](add-two-numbers)
66
- [Alien Dictionary](alien-dictionary)
7-
- [Balanced Binary Tree](balanced-binary-tree)
87
- [Best Time to Buy and Sell Stock II](best-time-to-buy-and-sell-stock-ii)
98
- [Binary Tree Level Order Traversal](binary-tree-level-order-traversal)
109
- [Clone Graph](clone-graph)
@@ -72,6 +71,7 @@
7271
- [Spiral Matrix](spiral-matrix)
7372
- [Subsets](subsets)
7473
- [Subsets II](subsets-ii)
74+
- [Subtree of Another Tree](subtree-of-another-tree)
7575
- [Target Sum](target-sum)
7676
- [Time-Based Key Value Store](time-based-key-value-store)
7777
- [Top K Frequent ELements](top-k-frequent-elements)

0 commit comments

Comments
 (0)