Same Tree
Given the roots of two binary trees p and q, return true if they are structurally identical and every corresponding pair of nodes has the same value, and false otherwise.
Open official problem prompt ↗Decide whether two binary trees are exact copies of each other in both shape and node values.
Like overlaying two transparent sheets each printed with a tree: they are 'the same' only if every branch lines up and every label matches when stacked.
- Input
- p = [1, 2, 3], q = [1, 2, 3]
- Output
- true
- Why
- Both trees have root 1 with left child 2 and right child 3, matching in shape and every value.
The number of nodes in each tree is in the range [0, 100]-10^4 <= Node.val <= 10^4