47. Same Tree
Easy · Binary Tree
Given two binary trees, determine if they are the same. Two binary trees are the same if they are structurally identical and the nodes have the same values.
Trees are represented as level-order arrays where null represents a missing node. For example, [1, 2, 3] represents a tree with root 1, left child 2, and right child 3. [1, 2, null, 4] represents a tree with root 1, left child 2, and left child of 2 is 4.
Examples
Example 1 Input: tree1 = [1, 2, 3], tree2 = [1, 2, 3] Output: true Explanation: Both trees have the same structure and node values: root 1 with left child 2 and right child 3.
Example 2 Input: tree1 = [1, 2], tree2 = [1, null, 2] Output: false Explanation: The first tree has a left child 2, while the second tree has a right child 2. They are structurally different.
Constraints
- Standard input/output constraints apply