POJ 2255 Tree Recovery

来源:互联网 发布:国家数据分析 编辑:程序博客网 时间:2024/05/29 17:07

Tree Recovery

Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8155 Accepted: 5175

Description

Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes.
This is an example of one of her creations:
                                               D                                              / \                                             /   \                                            B     E                                           / \     \                                          /   \     \                                         A     C     G                                                    /                                                   /                                                  F

    To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG.
She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).

    Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree.
However, doing the reconstruction by hand, soon turned out to be tedious.
So now she asks you to write a program that does the job for her!

Input

    The input will contain one or more test cases.
Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.)
Input is terminated by end of file.

Output

    For each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root).

Sample Input

DBACEGF ABCDEFGBCAD CBAD

 

Sample Output

ACBFGEDCDAB
 
package poj;import java.util.Scanner;/** * @description 北京大学 OJ 2255  * 跟据树的前序和中序遍历, 算出树的后序遍历。 * @technique递归。 * @version 1.0 * @date20120806 * @time10:54 * @author Alex * */public class Poj2255_20120806_0 {public static void main(String[] args) {Scanner in = new Scanner(System.in);String preOrder, inOrder;Node root;while(in.hasNext()){preOrder = in.next();//输入前序inOrder = in.next();//输入后序。//root = getRoot(preOrder, 0, inOrder, 0, inOrder.length());  //递归获得树根。root = getRoot(preOrder, inOrder);lastOrder(root);//后序遍历树。System.out.println();       //换行。}}/** * 递归函数,获得一棵树,返回根结点。 * @param preStr  前序序列。 * @param ps//前序序列开始位置。可不用。因为每次都从 0 位开始。(程序修改遗留); * @param midStr //中序序列。 * @param ms  //后序序列开始位置。 可不用。因为每次都从 0 位开始遍历(程序修改遗留);。 * @param me //后序序列结束位置。 可不用。因为最多到结束位置。(程序修改遗留); * @return 树根结点。 */@SuppressWarnings("unused")private static Node getRoot(String preStr,int ps,String midStr, int ms, int me){int i;for(i = ms; i < me; ++i){if(midStr.charAt(i) == preStr.charAt(ps)){Node node = new Poj2255_20120806_0().new Node(midStr.charAt(i));String pre_0 = preStr.substring(1,i+1);   //获得左子树的前序序列。String pre_1 = preStr.substring(i+1,preStr.length());//获得右子树的前序序列。String mid_0 = midStr.substring(0,i); //获得右子树的中序序列。String mid_1 = midStr.substring(i+1,midStr.length()); // 获得右子树的中序序列。Node left = getRoot(pre_0, 0 , mid_0, 0, mid_0.length()); //递归生成左子树。Node right = getRoot(pre_1,0, mid_1, 0, mid_1.length()); //递归生成右子树。node.setLeft(left); //设置根结点左子树。node.setRight(right);//设置根结点右子树。return node; //返回根结点。}}return null;}/** * 先看完上一重载函数后,再看本函数。这是上一函数的重写,省去了很多东西。 * @param preStr * @param midStr * @return */private static Node getRoot(String preStr, String midStr){int i;for(i = 0; i < midStr.length(); ++i){if(midStr.charAt(i) == preStr.charAt(0)){Node root = new Poj2255_20120806_0().new Node(midStr.charAt(i));Node left = getRoot(preStr.substring(1,i+1), midStr.substring(0,i));Node right = getRoot(preStr.substring(i+1,preStr.length()), midStr.substring(i+1,midStr.length()));root.setLeft(left);root.setRight(right);return root;}}return null;}private static void lastOrder(Node root){Node p = root;last(p);}private static void last(Node p){if(p!= null){last(p.getLeft());last(p.getRight());System.out.print(p.getValue());}}private class Node{private char value;private Node left;private Node right;private Node parent;public Node(){}public Node(char value){this.value = value;this.left = null;this.right = null;this.parent = null;}public char getValue() {return value;}public void setValue(char value) {this.value = value;}public Node getLeft() {return left;}public void setLeft(Node left) {this.left = left;}public Node getRight() {return right;}public void setRight(Node right) {this.right = right;}public Node getParent() {return parent;}public void setParent(Node parent) {this.parent = parent;}}}

原创粉丝点击