-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTraversal_Tree.cc
More file actions
58 lines (52 loc) · 1.02 KB
/
Copy pathTraversal_Tree.cc
File metadata and controls
58 lines (52 loc) · 1.02 KB
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
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct TreeNode {
TreeNode(int v): value(v) {
left = NULL;
right = NULL;
}
int value;
TreeNode *child;
TreeNode *brother;
}
int main(int argc, char **argv) {
int K = 0;
cin >> K;
if (K == 0) { cout << -1 << endl;
return 0;
}
// create LCRS Tree
TreeNode root(1);
TreeNode *parent = &root;
int v1, v2;
for (int i=0; i<(K-1); ++i) {
bool first = true;
cin >> v1 >> v2;
int sibcount = 0;
while (i == v1) {
TreeNode *n = new TreeNode(v2);
if (first) {
parent->brother = n;
parent = parent->brother;
first = false;
} else {
parent->right = n;
parent = parent->brother;
}
++sibcount;
cin >> v1 >> v2;
}
parent = find(v1);
i += sibcount;
}
// Traversal tree according to given leaf sequence
vector<int> seq;
int t = 0;
while (cin >> t) {
seq.push_back(t);
if (cin.get() == '\n') break;
}
return 0;
}