Symmetric Tree
Easy Trees
root is the root of a binary tree, which may be empty.
The tree is symmetric when it is its own mirror image: folded along a line down through the root, every node lands on a node of the same value on the other side. So the root's right subtree is its left subtree flipped from left to right, with the same values, and where a child is missing on one side, its mirror image is missing too.
Return true when the tree is symmetric and false when it is not. The empty tree, and a tree of one node, are symmetric.
Example 1
Fold the tree along a line down through 6. The two 3s meet. The 8 on the far left, under the left 3, meets the 8 on the far right, under the right 3. Below them, 2 meets 2 and 5 meets 5, each on the other side.
Example 2
Example 3
Constraints
0 ≤ number of nodes ≤ 10⁵-10⁴ ≤ node.val ≤ 10⁴
Solve it here
Write it in Python, Java or C++, run it on your own cases, and submit against hidden tests.
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
See the AI roundWatch the explanation
More Trees
- Lowest Common Ancestor of a Binary TreeAI round · Medium
- Validate Binary Search TreeAI round · Medium
- Binary Tree Level Order TraversalAI round · Medium
- Binary Tree Vertical Order TraversalMedium
- Find Leaves of Binary TreeMedium