-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsSameTree.py
More file actions
57 lines (43 loc) · 1.83 KB
/
IsSameTree.py
File metadata and controls
57 lines (43 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
################################################# Depth First Search ##########################################################
########## Time Complexity: O(n) ########## Space Complexity: O(d = depth of the tree) ##########
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
if not p and not q:
return True
elif not p:
return False
elif not q:
return False
stack1, stack2 = [p], [q]
while stack1 and stack2:
node1 = stack1.pop()
node2 = stack2.pop()
if node1 and node2:
if node1.val != node2.val:
return False
stack1.append(node1.left)
stack1.append(node1.right)
stack2.append(node2.left)
stack2.append(node2.right)
elif node1 or node2:
return False
return True
################################################# Recursion ##########################################################
########## Time Complexity: O(n) ########## Space Complexity: O(h = height of the tree) ###########
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
if not q and not p: return True
if p and q and p.val == q.val:
return (self.IsSameTree(p.left, q.left) and self.IsSameTree(p.right, q.right))