-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree_exam_5.cpp
More file actions
110 lines (87 loc) · 1.57 KB
/
Copy pathtree_exam_5.cpp
File metadata and controls
110 lines (87 loc) · 1.57 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int val;
Node *left = nullptr;
Node *right = nullptr;
Node(int val)
{
this->val = val;
}
};
Node *inputTree()
{
Node *root = nullptr;
int val;
cin >> val;
if (val == -1)
{
return root;
}
root = new Node(val);
queue<Node *> q;
q.push(root);
while (!q.empty())
{
Node *temp = q.front();
q.pop();
int x = -1, y = -1;
cin >> x >> y;
if (x != -1)
{
temp->left = new Node(x);
q.push(temp->left);
}
if (y != -1)
{
temp->right = new Node(y);
q.push(temp->right);
}
}
return root;
}
void printLeft(Node *root)
{
if (!root)
return;
if (root->left)
{
printLeft(root->left);
}
else if (root->right)
{
printLeft(root->right);
}
cout << root->val << " ";
}
void printRight(Node *root)
{
if (!root)
return;
cout << root->val << " ";
if (root->right)
{
printRight(root->right);
}
else if (root->left)
{
printRight(root->left);
}
}
int main()
{
Node* root = inputTree();
printLeft(root->left);
if(root){
cout<<root->val<<" ";
}
printRight(root->right);
// Node *root = new Node(10);
// root->left = new Node(20);
// root->left->left = new Node(40);
// root->left->left->left = new Node(90);
// cout << root->left->left->left->val << endl;
return 0;
}