-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.cpp
44 lines (37 loc) · 829 Bytes
/
main.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
/* you only have to complete the function given below.
Node is defined as
struct node
{
int data;
node* left;
node* right;
};
*/
#include <iostream>
using namespace std;
// Recursive approach
void Inorder(node *root) {
if (root != NULL) {
Inorder(root->left);
cout << root->data << " ";
Inorder(root->right);
}
}
#include <stack>
// Iterative approach
void Inorder(node *root) {
stack<node*> s;
node* current = root;
do {
while (current != NULL) {
s.push(current);
current = current->left;
}
if (current == NULL && !s.empty()) {
current = s.top();
s.pop();
cout << current->data << " ";
current = current->right;
}
} while (current != NULL || !s.empty());
}