Tree in order traversal.

Tree in order traversal, return the next node.

C++:
01 Node* inorderTravelNext(Node* head, Node* p)
02 {
03      stack <Node*> S;
04      S.push(head);
05      Node* prv=head;
06      bool flag=false;
07      while(!S.empty()){
08          Node* tmp=S.top();
09          S.pop();
10          if((prv==head)||(prv->left==tmp)||(prv->right==tmp)){
11              if(tmp->right)
12                  S.push(tmp->right);
13              S.push(tmp);
14              if(tmp->left)
15                  S.push(tmp->left);
16              prv=tmp;
17              continue;
18          }
19          if(flag==true)
20              return tmp;
21          if(tmp==p)
22              flag=true;
23          cout<<tmp->value<<” “;
24          prv=tmp;
25      }
26 }
Advertisements
This entry was posted in Tree. Bookmark the permalink.

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