Same Tree
Compare two binary trees for structural and value equality
Python Code
5 self.right = right● 7class Solution:8 def isSameTree(self, p, q) -> bool:9 # Base case: both nodes are null10 if not p and not q:......15 return False16 17 # Values differ18 if p.val != q.val:19 return False20 ......22 left_same = self.isSameTree(p.left, q.left)23 if not left_same:24 return FalseCurrent Line (6): Function Entry
Tree P
Operation:Compare: p=1, q=1
Tree Q
Operation:Compare: p=1, q=1
Comparison Progress
Recursion Stack
Stack is empty. Click Next to begin!
Step Explanation
Ready To Compare Trees
Click "Next Step" to start pairwise recursion. We compare corresponding nodes in p and q together.
- > Both-null pair means this branch matches.
- > One-null pair means structure mismatch.
- > Non-null pair must match values before recursing.
Unvisited
Current Pair
Exploring Left
Exploring Right
Done