Skip to content

Commit 9032a92

Browse files
committed
added subtree of another tree solution
1 parent 3aa51b5 commit 9032a92

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Time O(mn)
2+
def is_subtree(root, sub_root):
3+
if not sub_root: return True
4+
if not root: return False
5+
6+
if is_same_tree(root, sub_root):
7+
return True
8+
9+
return is_subtree(root.left, sub_root) or is_subtree(root.right, sub_root)
10+
11+
def is_same_tree(s, q):
12+
if not s and not q: return True
13+
if s and q and s.val == q.val:
14+
return (is_same_tree(s.left, q.left) and is_same_tree(s.right, q.right))
15+
16+
return False

0 commit comments

Comments
 (0)