joj 2035 Leaf Nodes

joj 2035 Leaf Nodes
Leaf Nodes
Status In/Out TIME Limit MEMORY Limit Submit Times Solved Users JUDGE TYPE
stdin/stdout 1s 10240K 222 102 Standard

Kate is learning about binary tree. She think she know everything you know about binary trees. Wait, you don't know binary tree? Find a book about data structures, and it will just take you about three minutes. Now here is a binary tree:

                                    3
/ \
/   \
2     4
/ \     \
/   \     \
0     1     6
/
/
5

Kate think she also know something that you may not notice. First, for some type of binary trees, only the leaf nodes have the meaning (leaf node is the node which has no sub trees, for the tree above, the leaf nodes are 0 1 5), an example is the Huffman Tree. Second, she guess that if you know the preorder traversal and the postorder traversal of a binary tree, you can ensure the leaf node of the tree, and their order.

For the tree above, the preorder travesal is 3 2 0 1 4 6 5 and the postorder travesal is 0 1 2 5 6 4 3, the leaf nodes in order(from left to right) are 0 1 5.

But now the problem is she just guess it, if you can find a way to print a tree's leaf nodes in order using its preorder traversal and postorder traversal, you can say "she is right!"

Input Specification

The input file will contain one or more test cases. In each test case there is a integer n (0<=n <= 10000), indicating the tree have n nodes, then followed two lists of integers, either contains n integers in the range of 0 and n-1 (both included), the first list is the preorder traversal, and the other is the postorder traversal.

Input is terminated by an interger -1;

Output Specification

For each test case, print the tree's leaf nodes in order, each in a line.

Sample Input

7
3 2 0 1 4 6 5
0 1 2 5 6 4 3
-1

Sample Output

0
1
5
根据一个重要结论,无论是先根还是后根遍历,左子树的结点总是出现在右子树结点的前面

                                 G  
                                /   \  
                              F        B  
                            /        /   \  
                          K         C      H  
                        /   \             /        
                      D       E         J  
                        \  
                          A  
                        /  
                      I  
   
  不论先根后根,左子树的结点总是出现在右子树结点的前面。  
  G为根树,先根次序时G后跟F,后根次序时F前有DIAEKF,故DIAEKF为G的左子树的结点,
  CJHB为G的右子树的结点。且左右子树的先根序为:FKDIAE,BCHJ。
  递归处理两子树即可搞定

void  find(int preb,int pree,int postb,int poste) 
   {
   int i=s(pre,post[poste-1]);
  int j=s(post,pre[preb+1]);
//添加处理的代码
 //判断是否有左/右支

    find(preb+1,i-1,postb,j);
  find(i,pree,j+1,poste-1);
   }
但是上面的思路是错误的!!!!!!!!!!!!!!
只知道先序和后序不能能推出树来  
   
  只有中序和先序或者中序和后序才可以  
   
  不然只知道根节点,但是哪些是左子树哪些是右子树就不知道了
比如先序时1234  
  后序是4321的二叉树有8种比如:  
      1                     1              
      \                   /  
        2               2  
    /                 /  
  3                 3  
    \             /  
      4         4

正确思路:先遍历后根次序,第一个一定为叶子,设为当前结点,然后依次检测,如果该点在先根序列中位于当前节点的后面,则为叶子节点,同时更新当前结点。

你可能感兴趣的:(joj 2035 Leaf Nodes)