If you are given two traversal sequences, can you construct the binary tree?
2014-09-30 21:16
791 查看
It depends on what traversals are given. If one of the traversal methods is Inorder then the tree can be constructed, otherwise not.
Therefore, following combination can uniquely identify a tree.
Inorder and Preorder.
Inorder and Postorder.
Inorder and Level-order.
And following do not.
Postorder and Preorder.
Preorder and Level-order.
Postorder and Level-order.
For example, Preorder, Level-order and Postorder traversals are same for the trees given in above diagram.
Preorder Traversal = AB
Postorder Traversal = BA
Level-Order Traversal = AB
So, even if three of them (Pre, Post and Level) are given, the tree can not be constructed.
Therefore, following combination can uniquely identify a tree.
Inorder and Preorder.
Inorder and Postorder.
Inorder and Level-order.
And following do not.
Postorder and Preorder.
Preorder and Level-order.
Postorder and Level-order.
For example, Preorder, Level-order and Postorder traversals are same for the trees given in above diagram.
Preorder Traversal = AB
Postorder Traversal = BA
Level-Order Traversal = AB
So, even if three of them (Pre, Post and Level) are given, the tree can not be constructed.
相关文章推荐
- [Tree]If you are given two traversal sequences, can you construct the binary tree?
- [面试题]Amazon: Given two binary trees,if the first tree is subtree of the second one
- 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
- 【DB2】If 'db2' is not a typo you can run the following command to lookup the package that contains the binary: command-not-found db2 bash: db2: command not found
- Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree andsum =
- Can't create a new thread (errno 11); if you are not out of available memory, you can consult the manual for a possible OS-dependent bug
- Given preorder and inorder traversal of a tree, construct the binary tree.
- can't create a new thread(errno 11) ;if you are not out of avaiable memory ,you can consult the manu
- Check if the given binary tree is BST or not.
- Given two binary trees, write a function to check if they are equal or not.
- Error:Connection timed out: connect. If you are behind an HTTP proxy, please configure the proxy set
- Data Structure Binary Tree: Construct Full Binary Tree from given preorder and postorder traversals
- Error:Connection timed out: connect. If you are behind an HTTP proxy, please configure the proxy set
- fs: re-evaluating native module sources is not supported. If you are using the graceful-fs module
- Data Structure Binary Tree: Construct Tree from given Inorder and Preorder traversals
- PLSQL result set exceeds the maximum size(100M)if necessary,you can explicitly confinue this query
- Error:Connection timed out: connect. If you are behind an HTTP proxy, please configure the proxy...
- anaconda2下面安装opencv2.4.13.4完成----解决默认安装的问题----Thefunction is not implemented. Rebuild the library with Windows, GTK+ 2.x orCarbon support. If you are on Ubuntu or Debian, install libgtk2.0‑dev and pkg
- You are to find all the two-word compound words in a dictionary. A two-word compound word is a word
- Given a binary tree, return the postorder traversal of its nodes' values.