Symmetric Tree

Easy#101tree nodes

You get the root of a binary tree. Say whether it is symmetric: folded along a vertical line through the root, its left half lands exactly on its right half, value on value and empty on empty. Our tree: 8 with two children 6; the left 6 has children 5 and 7, the right 6 has children 9 and 5 — the answer is false: the outer 5s mirror each other, but 7 and 9 do not.

input
root: a binary tree, written as on LeetCode: its values level by level, left to right, null for a missing child.
returns
true if the tree is symmetric around its center: its left subtree is the mirror image of its right one, in shape and in values; otherwise false.
constraints
  • 1 ≤ the number of nodes ≤ 1000
  • −100 ≤ Node.val ≤ 100
our 14 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
function isSymmetric(root) {

}
Ctrl ↵
format
reset

saved in this browser

14 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more depth-first search (dfs) problems

Pattern “Depth-first search (DFS)” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.