[Google] print the outline of a complete binary tree in anti-clockwise direction
2014-07-27 00:57
549 查看
Question:
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
Solution:
1.这题表明是个完全二叉树,我们可以考虑额外用一个数组保存树(充分利用题目中给定的条件).假定数组大小为n,
1)遍历所有左边节点.则是 1 ,2 ,4,8,....,k(k<=n).
2)遍历所有叶子节点,遍历叶子节点分两部分,第一部分从k后面开始直到n,接着是从n/2+1到k-1.
3)访问右边节点,n/2,n/4,n/8,...,直到1(当然不包括1)
2.如果该题不说是完全二叉树,那我们如何基于树本身来访问呢?道理也是如上述,
1)向下访问左边节点.
2)访问所有叶子节点.
3)向上访问右边节点
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
Solution:
1.这题表明是个完全二叉树,我们可以考虑额外用一个数组保存树(充分利用题目中给定的条件).假定数组大小为n,
1)遍历所有左边节点.则是 1 ,2 ,4,8,....,k(k<=n).
2)遍历所有叶子节点,遍历叶子节点分两部分,第一部分从k后面开始直到n,接着是从n/2+1到k-1.
3)访问右边节点,n/2,n/4,n/8,...,直到1(当然不包括1)
2.如果该题不说是完全二叉树,那我们如何基于树本身来访问呢?道理也是如上述,
1)向下访问左边节点.
2)访问所有叶子节点.
3)向上访问右边节点
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) { if(node == null) return; print_right(node->rignt); if (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); }
相关文章推荐
- Search the Nth element of Level M in a binary tree
- Print Nodes in Top View of Binary Tree
- ch4.6: find the LCA of 2 nodes in a binary tree with 3 different methods
- Find the common ancestor of two nodes in a binary tree
- Given preorder and inorder traversal of a tree, construct the binary tree.
- Simple implementation of the BinaryTree preorder traversal, inorder traversal, post traversal
- print a binary tree in anti-clockwise direction
- Find the nearest common ancestor of any two nodes in a binary search tree
- Lowest Common Ancestor of The Two Nodes in a Binary Tree
- print out the path from one node to another in binary tree
- Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe
- Print the numbers of form (2^i)*(5^j) in increasing order
- [geeks]Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST
- The most elegant way to print a Binary Tree
- the thinker of code complete -- general issues in using variables
- [面试题]Amazon: Given two binary trees,if the first tree is subtree of the second one
- com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException: This application is out of date, please click the refresh but
- The diameter of a binary tree
- Write a function to find the depth of a binary tree
- count the number of "1" in the binary figure