# LeetCode: Symmetric Tree Solution

Recursion

## Approach

Base cases:

• false
when either left or right is null
• true
when both left and right are null
• false
when both values of left and right are not equal

## Implementation

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}var isSymmetric = function (root) {2  if (!root) return true3
4  const recursion = (left, right) => {5    if ((!left && right) || (left && !right)) return false6    if (!left && !right) return true7    if (left.val !== right.val) return false8    return (9      recursion(left.left, right.right) && recursion(left.right, right.left)10    )11  }12
13  return recursion(root.left, root.right)14}```

Original problem

## Comments

Loading comments...

leetcode

tree

binary tree

recursion

## Next Post

LeetCode: Word Pattern

Oct 4, 2022

2 hash tables

## Previous Post

LeetCode: Implement Queue using Stacks

Sep 30, 2022

Code speaks for itself

Search Posts