-
Notifications
You must be signed in to change notification settings - Fork 0
Add unit tests for the "Subtree of Another Tree" problem. #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| * <p> | ||
| * 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). | ||
| * <p> | ||
| * Space complexity: O(M+N). | ||
| * There will be at most N recursive call to isSubtree. | ||
|
kobukuro marked this conversation as resolved.
Outdated
|
||
| * 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; | ||
|
Comment on lines
+43
to
+47
|
||
| } | ||
| if (this.isSameTree(root, subRoot)) { | ||
| return true; | ||
| } | ||
| return this.isSubtree(root.left, subRoot) || this.isSubtree(root.right, subRoot); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.