forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInOrderTraversal.cpp
More file actions
38 lines (35 loc) · 832 Bytes
/
InOrderTraversal.cpp
File metadata and controls
38 lines (35 loc) · 832 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
#include <iostream>
using namespace std;
class node {
public:
node *l_child;
node *r_child;
node *parent;
int value;
node() {}
};
// Nonrecursive inorder tree traversal without stack
void InOrderTraversal(node *root) {
node *prev = NULL;
node *next = NULL;
node *current = root;
while (current != NULL) {
if (prev == current->parent) {
prev = current;
next = current->l_child;
}
if ((next == NULL) || (prev == current->l_child)) {
printf("%d\n", current->value);
prev = current;
next = current->r_child;
}
if ((next == NULL) || (prev == current->r_child)) {
prev = current;
next = current->parent;
}
current = next;
}
}
int main() {
return 0;
}