diff --git a/src/SubtreeOfAnotherTree572.java b/src/SubtreeOfAnotherTree572.java deleted file mode 100644 index 79597c0..0000000 --- a/src/SubtreeOfAnotherTree572.java +++ /dev/null @@ -1,55 +0,0 @@ -// Tree, Depth-First Search, String Matching, Binary Tree, Hash Function -public class SubtreeOfAnotherTree572 { - public boolean isSameTree(TreeNode p, TreeNode q) { - if (p == null && q == null) { - return true; - } - if (p == null) { - return false; - } - if (q == null) { - return false; - } - if (p.val != q.val) { - return false; - } - return this.isSameTree(p.left, q.left) && this.isSameTree(p.right, q.right); - } - - /* - N is the number of nodes in the tree rooted at the root - M is the number of nodes in the tree rooted at subRoot - Time complexity: O(MN). - For every N node in the tree, - we check if the tree rooted at node is identical to subRoot. - This check takes O(M) time, where M is the number of nodes in subRoot. - Hence, the overall time complexity is O(MN). - - Space complexity: O(M+N). - There will be at most N recursive call to isSubtree. - Now, each of these calls will have M recursive calls to isSameTree. - Before calling isSameTree, our call stack has at most O(N) elements - and might increase to O(N+M) during the call. - After calling isSameTree, - it will be back to at most O(N) since all elements made by isSameTree are popped out. - Hence, the maximum number of elements in the call stack will be M+N. - */ - public boolean isSubtree(TreeNode root, TreeNode subRoot) { - if (subRoot == null) { - return true; - } - if (root == null) { - return false; - } - if (this.isSameTree(root, subRoot)) { - return true; - } - return this.isSubtree(root.left, subRoot) || this.isSubtree(root.right, subRoot); - } - - public static void main(String[] args) { - TreeNode root = new TreeNode(3, new TreeNode(4, new TreeNode(1), new TreeNode(2)), new TreeNode(5)); - TreeNode subRoot = new TreeNode(4, new TreeNode(1), new TreeNode(2)); - System.out.println(new SubtreeOfAnotherTree572().isSubtree(root, subRoot)); // true - } -} diff --git a/src/main/java/com/leetcode/easy/SubtreeOfAnotherTree572.java b/src/main/java/com/leetcode/easy/SubtreeOfAnotherTree572.java new file mode 100644 index 0000000..da8b5dc --- /dev/null +++ b/src/main/java/com/leetcode/easy/SubtreeOfAnotherTree572.java @@ -0,0 +1,54 @@ +// Tags: Tree, DFS +package com.leetcode.easy; + +import com.leetcode.datastructure.TreeNode; + +public class SubtreeOfAnotherTree572 { + public boolean isSameTree(TreeNode p, TreeNode q) { + if (p == null && q == null) { + return true; + } + if (p == null) { + return false; + } + if (q == null) { + return false; + } + if (p.val != q.val) { + return false; + } + return this.isSameTree(p.left, q.left) && this.isSameTree(p.right, q.right); + } + + /** + * N is the number of nodes in the tree rooted at the root + * M is the number of nodes in the tree rooted at subRoot + *
+ * Time complexity: O(M*N). + * For each of the N nodes in the tree, + * we check if the tree rooted at node is identical to subRoot. + * This check takes O(M) time, where M is the number of nodes in subRoot. + * Hence, the overall time complexity is O(M*N). + *
+ * Space complexity: O(M+N). + * There will be at most N recursive calls to isSubtree. + * Now, each of these calls will have M recursive calls to isSameTree. + * Before calling isSameTree, our call stack has at most O(N) elements + * and might increase to O(N+M) during the call. + * After calling isSameTree, + * it will be back to at most O(N) since all elements made by isSameTree are popped out. + * Hence, the maximum number of elements in the call stack will be M+N. + */ + public boolean isSubtree(TreeNode root, TreeNode subRoot) { + if (subRoot == null) { + return true; + } + if (root == null) { + return false; + } + if (this.isSameTree(root, subRoot)) { + return true; + } + return this.isSubtree(root.left, subRoot) || this.isSubtree(root.right, subRoot); + } +} diff --git a/src/test/java/com/leetcode/easy/SubtreeOfAnotherTree572Test.java b/src/test/java/com/leetcode/easy/SubtreeOfAnotherTree572Test.java new file mode 100644 index 0000000..ff623bb --- /dev/null +++ b/src/test/java/com/leetcode/easy/SubtreeOfAnotherTree572Test.java @@ -0,0 +1,43 @@ +package com.leetcode.easy; + +import com.leetcode.datastructure.TreeNode; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class SubtreeOfAnotherTree572Test { + private SubtreeOfAnotherTree572 solution; + + @BeforeEach + void setUp() { + solution = new SubtreeOfAnotherTree572(); + } + + @Test + void testIsSubtree_Example1() { + TreeNode root = new TreeNode(3, + new TreeNode(4, new TreeNode(1), new TreeNode(2)), + new TreeNode(5)); + TreeNode subRoot = new TreeNode(4, + new TreeNode(1), + new TreeNode(2)); + + boolean result = solution.isSubtree(root, subRoot); + + assertTrue(result); + } + + @Test + void testIsSubtree_Example2() { + TreeNode root = new TreeNode(3, + new TreeNode(4, new TreeNode(1), new TreeNode(2, null, new TreeNode(0))), + new TreeNode(5)); + TreeNode subRoot = new TreeNode(4, new TreeNode(1), new TreeNode(2)); + + boolean result = solution.isSubtree(root, subRoot); + + assertFalse(result); + } +}