FastPrepSymmetric Binary Tree

Symmetric Binary Tree

Figma logoFigma● EasyINTERNONSITE INTERVIEW
Learn

Problem statement

Given the root of a binary tree, return true when the tree is symmetric around its center and false otherwise.

Two subtrees mirror each other when their root values are equal, the left subtree of one mirrors the right subtree of the other, and the right subtree of one mirrors the left subtree of the other.

Function

isSymmetric(root: TreeNode) → boolean

Examples

Example 1

root = [1,2,2,3,4,4,3]return = true

The left and right subtrees have equal values in mirrored positions.

Example 2

root = [1,2,2,null,3,null,3]return = false

The two nodes with value 3 occupy the same side of their parents instead of mirrored sides.

Example 3

root = []return = true

An empty tree has no mismatched pair, so it is symmetric.

Constraints

  • The tree contains at most 10^5 nodes.
  • Each node value fits in a signed 32-bit integer.
  • The empty tree is symmetric.

More Figma problems

See Figma hiring insights
public boolean isSymmetric(TreeNode root) {
    // Write your code here.
}
root[1,2,2,3,4,4,3]
expectedtrue
Checking account…