주어진 두 개의 트리를 같은 순서대로 동시에 순회한다.
순회하면서 두 트리 노드 값이 같은지 확인한다.
두 트리 노드 값이 None 이면 같다고 처리한다.
class Solution:
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
if(p is None and q is None): return True
if(p is None or q is None or p.val != q.val): return False
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
< English (feedback by ChatGPT) >
We Iterate through the two given trees simultaneously as a same step.
(We iterate through the two given trees simultaneously in the same order.)
While iteration, we can check whether the two nodes of each trees have the same values.
(During the traversal, we check whether the corresponding nodes in each tree have the same values.)
It's the same when the both of two tree node values are None.
(If both nodes are None, we consider them equal.)
'Coding Interview' 카테고리의 다른 글
[leetcode] 110. Balanced Binary Tree (0) | 2025.05.06 |
---|---|
[leetcode] 108. Convert Sorted Array to Binary Search Tree (0) | 2025.05.06 |
[leetcode] 70. Climbing Stairs (0) | 2025.05.05 |
[leetcode] 21. Merge Two Sorted Lists (0) | 2025.05.03 |
[leetcode] 26. Remove Duplicates from Sorted Array (0) | 2025.05.01 |