Skip to content

Commit

Permalink
Create 572-Subtree-of-Another-Tree.java
Browse files Browse the repository at this point in the history
  • Loading branch information
SharmaTushar1 authored Jul 6, 2022
1 parent 3a5dffc commit af9b75f
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions java/572-Subtree-of-Another-Tree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
if (root == null && subRoot == null) return true;
if (root == null || subRoot == null) return false;
if (root.val == subRoot.val) {
return (isSubtree(root.left, subRoot.left) && isSubtree(root.right, subRoot.right));
} else {
return (isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot));
}
}
}

0 comments on commit af9b75f

Please sign in to comment.