-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiterativePreorder.cpp
More file actions
48 lines (47 loc) · 914 Bytes
/
iterativePreorder.cpp
File metadata and controls
48 lines (47 loc) · 914 Bytes
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
47
48
#include<bits/stdc++.h>
using namespace std;
struct Node{
int key;
struct Node* left;
struct Node* right;
Node(int key)
{
this->key=key;
left=right=NULL;
}
};
void iterativePreorder(struct Node* root)
{
if(root==NULL)
return;
stack<Node*> st;
Node* curr=root;
while(curr!=NULL||st.empty()==false)
{
while(curr!=NULL)
{
cout<<(curr->key)<<" ";
if(curr->right!=NULL)
st.push(curr->right);
curr=curr->left;
}
if(st.empty()==false){
curr=st.top();
st.pop();
}
}
}
int main()
{
struct Node* root=new Node(10);
root->left=new Node(20);
root->right=new Node(30);
root->left->left=new Node(40);
root->left->right=new Node(50);
root->right->left=new Node(60);
root->left->left->left=new Node(70);
root->left->left->right=new Node(80);
root->left->right->right=new Node(90);
iterativePreorder(root);
return 0;
}