File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include < vector>
3+
4+ struct Node
5+ {
6+ int val;
7+ int left;
8+ int right;
9+ };
10+
11+ class Tree
12+ {
13+ public:
14+ Tree (int n) {
15+ _tree.resize (n);
16+ int val, left, right;
17+ for (int i = 0 ; i < n; ++i) {
18+ std::cin >> val >> left >> right;
19+ _tree[i].val = val;
20+ _tree[i].left = left;
21+ _tree[i].right = right;
22+ }
23+ }
24+ ~Tree () {}
25+ void printInOrder (int v) {
26+ if (v == -1 )
27+ return ;
28+ printInOrder (_tree[v].left );
29+ std::cout << _tree[v].val << " " ;
30+ printInOrder (_tree[v].right );
31+ }
32+
33+ void printPreOrder (int v) {
34+ if (v == -1 )
35+ return ;
36+ std::cout << _tree[v].val << " " ;
37+ printPreOrder (_tree[v].left );
38+ printPreOrder (_tree[v].right );
39+ }
40+
41+ void printPostOrder (int v) {
42+ if (v == -1 )
43+ return ;
44+ printPostOrder (_tree[v].left );
45+ printPostOrder (_tree[v].right );
46+ std::cout << _tree[v].val << " " ;
47+ }
48+ private:
49+ std::vector<Node> _tree;
50+ };
51+
52+ int main (void ) {
53+ int n;
54+ std::cin >> n;
55+ Tree tree (n);
56+ tree.printInOrder (0 );
57+ std::cout << std::endl;
58+ tree.printPreOrder (0 );
59+ std::cout << std::endl;
60+ tree.printPostOrder (0 );
61+ std::cout << std::endl;
62+ return 0 ;
63+ }
You can’t perform that action at this time.
0 commit comments