Loading...
Loading...
You are given the root of a binary tree, root. Return whether it is symmetric.
A tree is symmetric if it looks the same as its own mirror image around the root: the left subtree is a mirror of the right subtree, matching both shape and node values.
Your function receives root as a TreeNode. The node type is provided for you, with val, left, and right fields.
The examples below write the tree as its level-order traversal, where null marks a missing child of a listed node and trailing nulls are omitted. That is only how the tree is displayed; the decoding is done for you.
root is in the range [1,105].Node.val ≤109The left subtree [2,3,4] mirrors the right subtree [2,4,3]: both roots are 2, and 3 on the far left matches 3 on the far right while 4 matches 4 in the middle.
Both subtrees have the same values, 2 with a child 3, but the 3 hangs off the same side (the right) under each 2 instead of mirrored sides, so the shapes do not actually mirror each other.
A single node has no children to compare, so it is trivially symmetric.