forked from leocamello/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
42 lines (35 loc) · 735 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
/* 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 Preorder(node *root) {
if (root != NULL) {
cout << root->data << " ";
Preorder(root->left);
Preorder(root->right);
}
}
#include <stack>
// Iterative approach
void Preorder(node *root) {
stack<node*> s;
s.push(root);
node* current;
while (!s.empty()) {
current = s.top();
s.pop();
if (current != NULL) {
cout << current->data << " ";
s.push(current->right);
s.push(current->left);
}
}
}