Symmetric Tree

Posted on


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

For example, this binary tree is symmetric:

   / \
  2   2
 / \ / \
3  4 4  3

But the following is not:

   / \
  2   2
   \   \
   3    3


Screen Shot 2015-10-30 at 3.26.33 PM

Use recursion. Be clear about the ending recursion conditions.

  1. One of left and right child tree is null?
  2. Both child trees are null?

How to pass on the nodes for detection? Use recursion to think about it for you! Just generalize the simplest condition for the root node and apply this as the recursion return statement.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s