#include <iostream>
#include <stack>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
Node(int val) : data(val), left(nullptr), right(nullptr) {}
};
void printLeftBoundary(Node* root) {
if (!root || (!root->left && !root->right))
return;
cout << root->data << " ";
if (root->left)
printLeftBoundary(root->left);
else
printLeftBoundary(root->right);
}
void printLeaves(Node* root) {
if (!root)
return;
printLeaves(root->left);
if (!root->left && !root->right)
cout << root->data << " ";
printLeaves(root->right);
}
void printRightBoundary(Node* root) {
if (!root || (!root->left && !root->right))
return;
if (root->right)
printRightBoundary(root->right);
else
printRightBoundary(root->left);
cout << root->data << " ";
}
void printBoundary(Node* root) {
if (!root)
return;
cout << root->data << " ";
printLeftBoundary(root->left);
printLeaves(root->left);
printLeaves(root->right);
printRightBoundary(root->right);
}
int main() {
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
root->right->left = new Node(6);
root->right->right = new Node(7);
printBoundary(root);
return 0;
}