Skip to content

Commit af9b75f

Browse files
Create 572-Subtree-of-Another-Tree.java
1 parent 3a5dffc commit af9b75f

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

java/572-Subtree-of-Another-Tree.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
3+
if (root == null && subRoot == null) return true;
4+
if (root == null || subRoot == null) return false;
5+
if (root.val == subRoot.val) {
6+
return (isSubtree(root.left, subRoot.left) && isSubtree(root.right, subRoot.right));
7+
} else {
8+
return (isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot));
9+
}
10+
}
11+
}

0 commit comments

Comments
 (0)