-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinorder2.cpp
65 lines (61 loc) · 858 Bytes
/
inorder2.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<iostream>
using namespace std;
struct Tree
{
int val;
Tree*parent;
Tree*left;
Tree*right;
};
void inorder(Tree*root)
{
Tree*pre;
Tree*curr=root;
while(curr)
{
if(pre==curr->parent)
{
if(curr->left)
{
pre=curr;
curr=curr->left;
}
else
pre=NULL;
}
else if(pre==curr->left)
{
cout<<curr->val<<endl;
if(curr->right)
{
pre=curr;
curr=curr->right;
}
else
{
pre=curr;
curr=curr->parent;
}
}
else if(pre==curr->right)
{
pre=curr;
curr=curr->parent;
}
}
}
main()
{
Tree*root=new Tree();
root->val=3;
root->left=new Tree();
root->left->val=1;
root->left->parent=root;
root->right=new Tree();
root->right->val=5;
root->right->parent=root;
root->left->right=new Tree();
root->left->right->val=0;
root->left->right->parent=root->left;
inorder(root);
}