Data Structures19 sections · 729 units
Open in Course

Problem - Same Tree

Check structural equality

Given roots of two binary trees, check if they are structurally identical and have the same node values. Example: Trees [1,2,3][1,2,3] and [1,2,3][1,2,3] are the same.

Trees [1,2][1,2] and [1,null,2][1,null,2] are not. Same values but different structure. This extends the recursive pattern: instead of computing a value, you're computing a boolean.

Both subtrees must match for the trees to be equal. Constraints: up to 100100 nodes per tree, values from 104-10^4 to 10410^4.