We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 32deff5 commit 532c978Copy full SHA for 532c978
subtree-of-another-tree/samthekorean.py
@@ -0,0 +1,29 @@
1
+# Time complexity : O(n*m)
2
+# Space complexity : Space complexity: O(h) h is the height of the call stack during the recursive traversal.
3
+class Solution:
4
+ def isSubtree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
5
+ if not q:
6
+ return True
7
+
8
+ if not p:
9
+ return False
10
11
+ if self.isSameTree(p, q):
12
13
14
+ return self.isSubtree(p.left, q) or self.isSubtree(p.right, q)
15
16
+ def isSameTree(self, p, q) -> bool:
17
+ if not p and not q:
18
19
20
+ if not p and q:
21
22
23
+ if p and not q:
24
25
26
+ if p.val != q.val:
27
28
29
+ return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
0 commit comments