LeetCode 101. Symmetric Tree

Description

https://leetcode.com/problems/symmetric-tree/description/

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

But the following [1,2,2,null,3,null,3] is not:

Note:
Bonus points if you could solve it both recursively and iteratively.

Explanation

The key point is to implement isMirror(TreeNode node1, TreeNode node2) function.

There are two scenarios for isMirror(TreeNode node1, TreeNode node2) to return true:

  1. when node1 and node2 are both null
  2. when node1 and node2 aren’t null, node1 and node2 should have same value and node1’s left should be the mirror for node2’s right and node1’s right should be the mirror node2.left subtrees.

Video Tutorial

Java Solution

Leave a Reply

Your email address will not be published. Required fields are marked *