Binary tree in order, without using recursion

An interesting question I came across recently:

Pb:  Write a function to print binary tree in order, without using recursion. You are given a stack object, which has methods pop and push

If you are well versed in recursion, this problem may be tricky to think through at first.

Basically one needs to develop insight about a few things :

  1. The terminating condition.
  2. What to do once you have pop’d a node.

 

Code

 

 

 

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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