Loading...
Loading...
You are given two binary trees, root0 and root1. Return whether they are identical.
Two trees are identical when they have exactly the same shape and the same value at every position. A position is reached from the root by a fixed sequence of moves to a left or right child. For every position, either both trees have a node there or neither does. When both do, the two nodes must hold the same value.
Two empty trees are identical. An empty tree is never identical to a tree with at least one node.
Your function receives root0 and root1 as TreeNode values (or the empty tree, as null). The node type is provided for you, with val, left, and right fields.
The examples below write trees as their level-order traversal, where null marks a missing child of a listed node and trailing nulls are omitted. That is only how trees are displayed; the decoding is done for you.
root0.root1.Node.val ≤104Both trees have root `1` with left child `2` and right child `3`. The shapes match and every position holds the same value, so the answer is `true`.
In `root0`, the node `2` is the left child of the root. In `root1`, it is the right child. The values agree, but the shapes differ, so the answer is `false`.
The shapes match, but the root's left child holds `2` in `root0` and `1` in `root1`. One mismatched value is enough, so the answer is `false`.