Skip to content

Commit e838854

Browse files
authored
Update Same Binary Tree - Leetcode 100.py
1 parent 298bebc commit e838854

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed
Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
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
17
class Solution:
28
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
3-
4-
def balanced(p, q):
5-
if not p and not q:
6-
return True
7-
8-
if (p and not q) or (q and not p):
9-
return False
10-
11-
if p.val != q.val:
12-
return False
13-
14-
return balanced(p.left, q.left) and balanced(p.right, q.right)
15-
16-
return balanced(p, q)
9+
# 1: Both Null
10+
if not p and not q:
11+
return True
12+
13+
# 2: One is Null
14+
if (p and not q) or (q and not p):
15+
return False
16+
17+
# 3. Values Mismatch
18+
if p.val != q.val:
19+
return False
20+
21+
return self.isSameTree(p.left, q.left) and \
22+
self.isSameTree(p.right, q.right)
1723

1824
# Time Complexity: O(n)
1925
# Space Complexity: O(h) { here "h" is the height of the tree }

0 commit comments

Comments
 (0)