bool isLeaf(Node *root) {
if(root->left == NULL && root->right == NULL)
return true;
return false;
}
void leftBoundry(Node *root, vector<int> &res) {
if(root == NULL || isLeaf(root)) return;
res.push_back(root->data);
if(root->left) leftBoundry(root->left, res);
else if(root->right) leftBoundry(root->right, res);
}
void leafBoundry(Node *root, vector<int> &res) {
if(root == NULL) return;
if(isLeaf(root)) {
res.push_back(root->data);
return;
}
leafBoundry(root->left, res);
leafBoundry(root->right, res);
}
void rightBoundry(Node *root, vector<int> &res) {
if(root == NULL || isLeaf(root)) return;
if(root->right) rightBoundry(root->right, res);
else if(root->left) rightBoundry(root->left, res);
res.push_back(root->data);
}
vector<int> boundaryTraversal(Node *root) {
// code here
vector<int> res;
if(!root) return res;
if(!isLeaf(root)) res.push_back(root->data);
leftBoundry(root->left, res);
leafBoundry(root, res);
rightBoundry(root->right, res);
return res;
}