bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 101. Symmetric Tree

101. Symmetric Tree

Leetcode Solutions

Recursive Approach to Check Symmetry of a Binary Tree

  1. Define a helper function isMirror that takes two nodes, left and right, as arguments.
  2. If both left and right are null, return true (base case for symmetry).
  3. If only one of left or right is null, return false (base case for asymmetry).
  4. If the values of left and right are not equal, return false.
  5. Recursively call isMirror on the left child of left and the right child of right, and on the right child of left and the left child of right.
  6. Return true if both recursive calls return true, otherwise return false.
  7. The main function isSymmetric calls isMirror with the left and right children of the root node.
UML Thumbnail

Iterative Approach to Check Symmetry of a Binary Tree

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...