http://www.geeksforgeeks.org/boundary-traversal-of-binary-tree/
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 #include <queue> 5 #include <stack> 6 #include <string> 7 #include <fstream> 8 #include <map> 9 using namespace std; 10 11 struct node { 12 int data; 13 struct node *left, *right; 14 node() : data(0), left(NULL), right(NULL) { } 15 node(int d) : data(d), left(NULL), right(NULL) { } 16 }; 17 18 void printleft(node *root) { 19 if (!root) return; 20 if (root->left) { 21 cout << root->data << " "; 22 printleft(root->left); 23 } 24 else if (root->right) { 25 cout << root->data << " "; 26 printleft(root->right); 27 } 28 } 29 30 void printleaf(node *root) { 31 if (!root) return; 32 printleaf(root->left); 33 if (!root->left && !root->right) cout << root->data << " "; 34 printleaf(root->right); 35 } 36 37 void printright(node *root) { 38 if (!root) return; 39 if (root->right) { 40 printright(root->right); 41 cout << root->data << " "; 42 } 43 else if (root->left) { 44 printright(root->left); 45 cout << root->data << " "; 46 } 47 } 48 49 void printboundary(node *root) { 50 if (!root) return; 51 cout << root->data << " "; 52 printleft(root->left); 53 printleaf(root); 54 printright(root->right); 55 } 56 57 int main() { 58 node *root = new node(20); 59 root->left = new node(8); 60 root->right = new node(22); 61 root->left->left = new node(4); 62 root->left->right = new node(12); 63 root->right->right = new node(25); 64 root->left->right->left = new node(10); 65 root->left->right->right = new node(14); 66 printboundary(root); 67 return 0; 68 }