void leafNodes(Node *root,vector<int> &list){
if(root == NULL){
return ;
}
if(!root->left and !root->right){
list.push_back(root->data);
return;
}
leafNodes(root->left,list);
leafNodes(root->right,list);
}
vector <int> printBoundary(Node *root)
{
vector<int> v;
if (root==NULL || (!root->right and !root->left)) {
if(root==NULL){
return v;
}
else{
v.push_back(root->data);
return v;
}
}
// If there is only 1 node print it
// and return
// if (!(root->left) && !(root->right)) {
// cout << root->data << endl;
// return;
// }
// List to store order of traversed
// nodes
vector<int> list;
list.push_back(root->data);
// Traverse left boundary without root
// and last node
Node* L = root->left;
while (L) {
if(L->left){
list.push_back(L->data);
L = L->left;
}
else if(L->right){
list.push_back(L->data);
L = L->right;
}
else {
break;
}
}
// BFS designed to only include leaf nodes
// queue<Node*> q;
// q.push(root);
// while (!q.empty()) {
// Node* temp = q.front();
// q.pop();
// if (!(temp->left) && !(temp->right)) {
// list.push_back(temp->data);
// }
// if (temp->left) {
// q.push(temp->left);
// }
// if (temp->right) {
// q.push(temp->right);
// }
// }
leafNodes(root,list);
// Traverse right boundary without root
// and last node
vector<int> list_r;
Node* R = root->right;
while (R) {
if(R->right){
list_r.push_back(R->data);
R = R->right;
}
else if(R->left){
list_r.push_back(R->data);
R = R->left;
}
else {
break;
}
}
// Reversing the order
reverse(list_r.begin(), list_r.end());
// Concatenating the two lists
list.insert(list.end(), list_r.begin(),
list_r.end());
// // Printing the node's data from the list
// for (auto i : list) {
// cout << i->data << " ";
// }
// cout << endl;
return list;
}