The second forum questions to prove safety offer 6: reconstruction of a binary tree (JAVA version)

Title: and enter the result in a preorder traversal of a binary tree in preorder traversal of the binary tree re-constructed. Suppose Results preorder traversal order and the input number is not included in duplicate. For example input sequence preorder traversal is {1,2,4,7,3,5,6,8} and {4,7,2,1,5,3,6,8} for the order traversal, the rebuild a binary tree, and outputs it to the first node.

Preorder traversal: root - "the left node -" right node

In order traversal: left node - "root -" the right node

Postorder: left node - "right node -" the root

In sequence preorder traversal of the binary tree, the value of the first digit is always the root of the tree. However, in order traversal, the value of the root node in the middle of the sequence, the value of the node to the left of the left subtree of the root node value, and the value of the node located to the right of the right subtree of the root node. So we need to scan in order traversal sequence, to find the value of the root node.

As shown, the first sequence preorder traversal number 1 is the value of a root node. Scan order traversal sequence, can determine the position of the root node value. The digital traversal sequence characteristics, the value of a first three digits are left subtree root node value of a point located behind the 1 values ​​are right subtree node.

 

Since the sequence preorder, three digital values ​​of the left subtree is the node, so the left subtree left a total of three sub-nodes. Similarly, pre-order traversing the sequence of three digits behind the root node is three nodes in the left subtree of the value, then all the following digits are right subtree node values. So we first preorder traversal sequence and two sequences, the sequences were found corresponding to the left and right subtrees.

 

Then find the root and traverse the left and right subtrees of a front left subtree sequence preorder and may be used to accomplish recursive

code show as below:

public  class ReconstructedBinaryTree1 { 

    public  static  void main (String [] args) {
         // binary sequence preorder 
        int [] = {preOrder . 1 , 2 , . 4 , . 7 , . 3 , . 5 , . 6 , . 8 };
         // binary tree sequence sequence 
        int [] = {inOrder . 4 , . 7 , 2 , . 1 , . 5 , . 3 , . 8 , . 6 }; 
        BinaryTreeNode the root= Reconstructe (preOrder, inOrder); 
        printPostOrder (the root); // order printing the binary 
    } 

    / * * 
     * complete binary tree traversal sequence reconstruction according to the preamble and the sequence 
     * 
     * @param preOrder 
     * sequence preorder traversal 
     * @param inOrder 
     sequence preorder * 
     * / 
    public  static BinaryTreeNode reconstructe ( int [] preOrder, int [] inOrder) {
         IF (preOrder == null || inOrder == null || preOrder.length == 0 || == inOrder.length 0 || preOrder.length! =inOrder.length) {
             return  null ; 
        } 

        // binary tree root 
        BinaryTreeNode the root = new new BinaryTreeNode (preOrder [ 0 ]); 
        root.setLeft ( null ); 
        root.setRight ( null ); 

        // number of left subtree 
        int = leftNum 0 ;
         for ( int I = 0 ; I <inOrder.length; I ++ ) {
             IF (root.getValue () == inOrder [I]) {
                 BREAK ; 
            } the else {
                leftNum ++ ; 
            } 
        } 
        // number of right subtree 
        int rightNum inOrder.length = - . 1 - leftNum; 

        // reconstructed left subtree 
        IF (leftNum> 0 ) {
             // left subtree sequence preorder 
            int [] = leftPreOrder new new  int [leftNum];
             // the left subtree motif sequence 
            int [] = leftInOrder new new  int [leftNum];
             for ( int I = 0 ; I <leftNum; I ++ ) { 
                leftPreOrder [I]PreOrder = [I + . 1 ]; 
                leftInOrder [I] = inOrder [I]; 
            } 
            BinaryTreeNode leftRoot = reconstructe (leftPreOrder, leftInOrder); // recursive construct left subtree 
            root.setLeft (leftRoot); 
        } 

        // reconstructed right sub tree 
        IF (rightNum> 0 ) {
             // right subtree sequence preorder 
            int [] = rightPreOrder new new  int [rightNum];
             // the right subtree motif sequence 
            int [] = rightInOrder new new  int [rightNum];
             for ( intI = 0 ; I <rightNum; I ++ ) { 
                rightPreOrder [I] = preOrder [leftNum + . 1 + I]; 
                rightInOrder [I] = inOrder [leftNum + . 1 + I]; 
            } 
            BinaryTreeNode rightRoot = reconstructe (rightPreOrder, rightInOrder); // recursive construct the right subtree 
            root.setRight (rightRoot); 
        } 
        return the root; 
    } 

    / * * 
     * after traversing binary tree (recursive) 
     * / 
    public  static  void printPostOrder (BinaryTreeNode the root) {
         IF (root != null) {
            printPostOrder(root.getLeft());
            printPostOrder(root.getRight());
            System.out.println(root.getValue());
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/xhlwjy/p/11257875.html