-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode-Solution-N100.py
More file actions
43 lines (32 loc) · 1.78 KB
/
LeetCode-Solution-N100.py
File metadata and controls
43 lines (32 loc) · 1.78 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
# LeetCode Problem N100: Same Tree
# Difficulty: Easy
from typing import Optional
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val: int = 0, left: 'TreeNode' | None = None, right: 'TreeNode' | None = None):
self.val = val
self.left = left
self.right = right
class Solution:
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
# (step 0:) Define a recursive helper function to compare two nodes.
def checker(p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
# (step 1:) If both nodes are None, the current branches match.
if not p and not q:
return True
# (step 2:) If exactly one is None, trees differ.
if not p or not q:
return False
# (step 3:) If node values differ, trees differ.
if p.val != q.val:
return False
# (step 4:) Recursively compare left subtrees AND right subtrees.
return checker(p.left, q.left) and checker(p.right, q.right)
# (step 5:) Start recursion from both roots and return the result.
return checker(p, q)
solution = Solution()
print(solution.isSameTree(TreeNode(1, TreeNode(2), TreeNode(3)), TreeNode(1, TreeNode(2), TreeNode(3)))) # Output: True
print(solution.isSameTree(TreeNode(1, TreeNode(2)), TreeNode(1, None, TreeNode(2)))) # Output: False
print(solution.isSameTree(TreeNode(1, TreeNode(2), TreeNode(1)), TreeNode(1, TreeNode(1), TreeNode(2)))) # Output: False
# Time Complexity: O(n), where n is the number of nodes in the trees.
# Space Complexity: O(h), where h is the height of the trees due to recursion stack.