Skip to content

Commit ccd8c3f

Browse files
solves subtree of another tree
1 parent 94c7624 commit ccd8c3f

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
| 561 | [Array Partition I](https://leetcode.com/problems/array-partition-i) | Easy | [![Java](assets/java.png)](src/ArrayPartitionI.java) [![Python](assets/python.png)](python/array_partiton_I.py) |
151151
| 563 | [Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt) | Easy | [![Java](assets/java.png)](src/BinaryTreeTilt.java) [![Python](assets/python.png)](python/binary_tree_tilt.py) |
152152
| 566 | [Reshape The Matrix](https://leetcode.com/problems/reshape-the-matrix) | Easy | [![Java](assets/java.png)](src/ReshapeTheMatrix.java) [![Python](assets/python.png)](python/reshape_the_matrix.py) |
153-
| 572 | [Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree) | Easy | |
153+
| 572 | [Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree) | Easy | [![Java](assets/java.png)](src/SubtreeOfAnotherTree.java) [![Python](assets/python.png)](python/subtree_of_another_tree.py) |
154154
| 575 | [Distribute Candies](https://leetcode.com/problems/distribute-candies) | Easy | |
155155
| 581 | [Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray) | Easy | |
156156
| 589 | [N-Ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal) | Easy | |

Diff for: python/subtree_of_another_tree.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Definition for a binary tree node.
2+
class TreeNode:
3+
def __init__(self, val=0, left=None, right=None):
4+
self.val = val
5+
self.left = left
6+
self.right = right
7+
8+
9+
class Solution:
10+
def areSame(self, root1: TreeNode, root2: TreeNode) -> bool:
11+
if root1 is None and root2 is None: return True
12+
if root1 is None or root2 is None: return False
13+
return root1.val == root2.val and self.areSame(root1.left, root2.left) and self.areSame(root1.right, root2.right)
14+
15+
def isSubtree(self, root: TreeNode, subRoot: TreeNode) -> bool:
16+
if root is None: return False
17+
if root.val == subRoot.val and self.areSame(root, subRoot): return True
18+
return self.isSubtree(root.left, subRoot) or self.isSubtree(root.right, subRoot)

Diff for: src/SubtreeOfAnotherTree.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class SubtreeOfAnotherTree {
2+
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
3+
if (root == null) return false;
4+
if (root.val == subRoot.val && areSame(root, subRoot)) {
5+
return true;
6+
}
7+
return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
8+
}
9+
10+
private boolean areSame(TreeNode root, TreeNode subTree) {
11+
if (subTree == null && root == null) return true;
12+
if (root == null || subTree == null) return false;
13+
return root.val == subTree.val && areSame(root.left, subTree.left) && areSame(root.right, subTree.right);
14+
}
15+
}

0 commit comments

Comments
 (0)