주어진 두 개의 트리를 같은 순서대로 동시에 순회한다.

순회하면서 두 트리 노드 값이 같은지 확인한다.

두 트리 노드 값이 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.)

 

 

 

 

+ Recent posts