Same Tree

Compare two binary trees for structural and value equality

Read Here
Step0/43
Comparingp=1, q=1
ResultChecking...
Back To Trees List

Python Code

5 self.right = right
7class Solution:
8 def isSameTree(self, p, q) -> bool:
9 # Base case: both nodes are null
10 if not p and not q:
......
15 return False
16
17 # Values differ
18 if p.val != q.val:
19 return False
20
......
22 left_same = self.isSameTree(p.left, q.left)
23 if not left_same:
24 return False
Current Line (6): Function Entry

Tree P

1234567
Operation:Compare: p=1, q=1

Tree Q

1234567
Operation:Compare: p=1, q=1

Comparison Progress

Current P

1

Current Q

1

Phase

Compare Nodes

Verdict

Checking...
👉 Step 1: Compare: p=1, q=1

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