Skip to content

Commit 33c2442

Browse files
authored
Create 572-subtree-of-another-tree.js
1 parent 23b2af9 commit 33c2442

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

572-subtree-of-another-tree.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} s
10+
* @param {TreeNode} t
11+
* @return {boolean}
12+
*/
13+
const isSubtree = function(s, t) {
14+
if (s == null) return false
15+
if (isSame(s, t)) return true
16+
return isSubtree(s.left, t) || isSubtree(s.right, t)
17+
};
18+
19+
function isSame(s, t) {
20+
if (s == null && t == null) return true
21+
if (s == null || t == null) return false
22+
if (s.val !== t.val) return false
23+
return isSame(s.left, t.left) && isSame(s.right, t.right)
24+
}

0 commit comments

Comments
 (0)