转自出处
Given a complete binary tree, print the outline of the tree in anti-clockwise direction, starting from the root. I.e. first print all the nodes on the left
edge of the tree going downwards, then print all the leaves going left to right (including leaves on both the last and the 2nd last level if necessary), then
print the nodes on the right edge of the tree going upwards.
Example tree:
A
/ \
/ \
B C
/ \ / \
D E F G
/ \ /
H I J
Expected Output: ABDHIJFGC
void print_left(Node *node) {
if(node == null)
return;
if (node -> left != NULL || node->right != NULL)
printf("%d ", node->v);
print_left(node->left);
}
void print_right(Node *node, bool first) {
if(node == null)
return;
print_right(node->rignt, false);
if (first == false && (node -> left != NULL || node->right != NULL))
printf("%d ", node->v);
}
void print_leafs(Node *node) {
if (node == NULL)
return;
if (node->left == NULL && node->right == NULL) {
printf("%d ", node->v);
}
print_leafs(node->left);
print_leafs(node->right);
}
void print_outline(Node *root) {
print_left(root);
print_leafs(root);
print_right(root, true);
}