-
Notifications
You must be signed in to change notification settings - Fork 0
/
boundary_traversal.cpp
112 lines (93 loc) · 2.09 KB
/
boundary_traversal.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
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
111
112
#include <bits/stdc++.h>
using namespace std;
class node{
public:
int data;
node *left;
node *right;
node(int x){
data = x;
left = NULL;
right = NULL;
}
};
node *buildTree(string s){
if(s.length() == 0 || s[0] == 'N') return NULL;
vector<string> ip;
istringstream iss(s);
for(string s; iss >> s;) ip.push_back(s);
node *root = new node(stoi(ip[0]));
queue<node *> queue;
queue.push(root);
int i = 1;
while(!queue.empty() && i < ip.size()){
node *cur = queue.front();
queue.pop();
string currval = ip[i];
if(currval != "N"){
cur->left = new node(stoi(currval));
queue.push(cur->left);
}
i++;
if(i >= ip.size()) break;
currval = ip[i];
if(currval != "N"){
cur->right = new node(stoi(currval));
queue.push(cur->left);
}
i++;
}
return root;
}
void printleaves(node *root, vector<int> &lea){
if(root == NULL) return;
printleaves(root->left, lea);
if(root->left == NULL && root->right == NULL)
lea.push_back(root->data);
printleaves(root->right, lea);
}
void printleft(node *root, vector<int> &left){
if(root == NULL) return;
if(root->left){
left.push_back(root->data);
printleft(root->left, left);
}
else if(root->right){
left.push_back(root->data);
printleft(root->right, left);
}
}
void printright(node *root, vector<int> &right){
if(root == NULL) return;
if(root->right){
printright(root->right, right);
right.push_back(root->data);
}
else if(root->left){
printright(root->left, right);
right.push_back(root->data);
}
}
vector <int> printBoundary(node *root)
{
if(root == NULL) return {};
vector<int> res;
res.push_back(root->data);
printleft(root->left, res);
printleaves(root->left,res);
printleaves(root->right, res);
printright(root->right, res);
return res;
//Your code here
}
int main(){
string s;
getline(cin,s);
node *root = buildTree(s);
vector<int> res = printBoundary(root);
for(int i = 0; i < res.size(); i++){
cout << res[i] << " ";
}
cout << endl;
return 0;
}