From ec82f2acd2cc841bd8d129112e2b19a7bdfd040f Mon Sep 17 00:00:00 2001 From: Peter Chen Date: Sun, 25 Jan 2026 10:06:19 +0800 Subject: [PATCH 1/3] Add unit tests for the "Subtree of Another Tree" problem. --- src/SubtreeOfAnotherTree572.java | 55 ------------------- .../easy/SubtreeOfAnotherTree572.java | 54 ++++++++++++++++++ .../easy/SubtreeOfAnotherTree572Test.java | 43 +++++++++++++++ 3 files changed, 97 insertions(+), 55 deletions(-) delete mode 100644 src/SubtreeOfAnotherTree572.java create mode 100644 src/main/java/com/leetcode/easy/SubtreeOfAnotherTree572.java create mode 100644 src/test/java/com/leetcode/easy/SubtreeOfAnotherTree572Test.java 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..a19ef69 --- /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 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(M*N). + *

+ * 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); + } +} 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); + } +} From 445d1fd04eed6c2bb1ef68bcfa4d25b41dfb4d7e Mon Sep 17 00:00:00 2001 From: Peter Chen Date: Sun, 25 Jan 2026 10:16:04 +0800 Subject: [PATCH 2/3] Fix grammar in time complexity comment for SubtreeOfAnotherTree. --- src/main/java/com/leetcode/easy/SubtreeOfAnotherTree572.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/leetcode/easy/SubtreeOfAnotherTree572.java b/src/main/java/com/leetcode/easy/SubtreeOfAnotherTree572.java index a19ef69..7c21d86 100644 --- a/src/main/java/com/leetcode/easy/SubtreeOfAnotherTree572.java +++ b/src/main/java/com/leetcode/easy/SubtreeOfAnotherTree572.java @@ -25,7 +25,7 @@ public boolean isSameTree(TreeNode p, TreeNode q) { * M is the number of nodes in the tree rooted at subRoot *

* Time complexity: O(M*N). - * For every N node in the tree, + * 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). From 3ed430caaa24aa2bd390611e9d7069c6a37b1088 Mon Sep 17 00:00:00 2001 From: Peter Chen Date: Sun, 25 Jan 2026 10:30:19 +0800 Subject: [PATCH 3/3] Fix grammar in space complexity comment for SubtreeOfAnotherTree. --- src/main/java/com/leetcode/easy/SubtreeOfAnotherTree572.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/leetcode/easy/SubtreeOfAnotherTree572.java b/src/main/java/com/leetcode/easy/SubtreeOfAnotherTree572.java index 7c21d86..da8b5dc 100644 --- a/src/main/java/com/leetcode/easy/SubtreeOfAnotherTree572.java +++ b/src/main/java/com/leetcode/easy/SubtreeOfAnotherTree572.java @@ -31,7 +31,7 @@ public boolean isSameTree(TreeNode p, TreeNode q) { * Hence, the overall time complexity is O(M*N). *

* Space complexity: O(M+N). - * There will be at most N recursive call to isSubtree. + * 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.