forked from sandeepshahi/Codes-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinorder.cpp
46 lines (41 loc) · 776 Bytes
/
inorder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Inorder traversal BST
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
node *left, *right;
node(int d)
{
data=d;
left=right=NULL;
}
};
unordered_map<node*,int> cnt;
void inorder(node* t)
{
stack<node* > s;
s.push(t);
while(!s.empty())
{
node* temp=s.top();
if(temp==NULL)
{
s.pop();
continue;
}
if(cnt[temp]==0){ s.push(temp->left);}
else if(cnt[temp]==1){ cout<<temp->data<<" ";}
else if(cnt[temp]==2){ s.push(temp->right);}
else s.pop();
cnt[temp]++;
}
}
int main()
{
node* t=new node(1);
t->left=new node(2);
t->right=new node(3);
t->left->left=new node(4);
inorder(t);
}