Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
解法1:
首先回想一下inorder和postorder的遍历顺序是什么样的。 inorder是left,root,right。 postorder是left,right,root。 那么发现postorder对应的最后一个数就是tree的root, 当我们找到乐root之后就可以在inorder里面找到root的位置,假设为i。
按照inorder的顺序,在i左面的都是root的左节点,而在i右面的都是root的右节点。
对于左右子树,我们只要在运行一次同样的算法,只是更新一下取值的范围(左右边界)就可以了。
C++
Java