Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<stack>
- using namespace std;
- class Node{
- public:
- int data;
- Node *left,*right;
- Node(int key){
- data=key;
- left=NULL;
- right=NULL;
- }
- };
- void Postorder(Node* root){
- if(root==NULL)
- return;
- stack<Node*>s;
- stack<int>out;
- s.push(root);
- while(!s.empty()){
- Node* temp=s.top();
- s.pop();
- out.push(temp->data);
- if(temp->left)
- s.push(temp->left);
- if(temp->right)
- s.push(temp->right);
- }
- while(!out.empty()){
- cout<<" "<<out.top();
- out.pop();
- }
- }
- int main(){
- Node* root=NULL;
- Node* newnode=new Node(1);
- root=newnode;
- root->left=new Node(9);
- root->right=new Node(3);
- (root->left)->left=new Node(2);
- (root->right)->right=new Node(4);
- Postorder(root);
- return 0;
- }
Add Comment
Please, Sign In to add comment