Binary Tree · Recursion · DFS
Same Tree
Given roots p and q, decide whether the two trees are structurally identical and have the same values at corresponding nodes.
Key concepts at a glance — for those who already know the basics.
Problem Statement
Given roots p and q, return true if the two binary trees are exactly the same.
Exactly the same means both structure and values match at every corresponding node position.
Example: p=[1,2,3], q=[1,2,3] => true; p=[1,2], q=[1,null,2] => false
01 · The definition
Same structure AND same values at every corresponding node
Both trees must be null at the same positions, and where they are not null, each pair of nodes must have equal values.
02 · The key insight
03 · Code (Python)
04 · Complexity
Practice on LeetCode
Try #572 (Subtree of Another Tree) next — it uses isSameTree as a direct helper.