To prove safety Offer-4. Reconstruction of a binary tree (C ++ / Java)

topic:

And enter the result in a preorder traversal of a binary tree in preorder traversal of the binary tree a rebuild. Suppose Results preorder traversal order and input of duplicate numbers are free. Before entering e.g. preorder traversal sequence {1,2,4,7,3,5,6,8} and {4,7,2,1,5,3,8,6} order traversal sequence, and the reconstructed binary tree return.

analysis:

And according to the preamble inorder traversal of the binary tree, binary tree reconstruction.

we know:

Preorder traversal: root, left subtree, right subtree.

Preorder: left subtree, root, right subtree.

Postorder: left subtree, right subtree, the root node.

Can be found, the first number is the root node in a preorder traversal of the entire number, and this number in order traversal, the array in turn divided into two parts, wherein the left subtree is the left, the right is the right subtree, in accordance with carved out of the left and right sub-tree, we can return to pre-order traversal, to view the first element of the left and right subtrees array, so that you can recursively to solve this problem.

We want to record a good sequence index at each recursive execution, this will help us to solve the problem that takes an array of sub-index, about a prelude to traverse the array index recorded as leftPre, rightPre, left and right in order to iterate the index is leftIn and rightIn.

Preorder traversal of the array index of the left subtree is left leftPre + 1, because the elements are leftPre refers root.

Preamble iterate left subtree is the right index leftPre + flag-leftIn, wherein the flag is an index in order to iterate the root, and the number of flag-leftIn exactly left subtree elements.

Preorder array index is leftPre left subtree and flag-1, because the flag is the root element we find, on the left is the new natural preorder the array.

Preamble index through the array left and right subtree is leftPre + flag-leftIn + 1, which is in fact the left and right sub-tree index is incremented by 1, because pre-order traversing the left and right subtrees are connected. rightPre naturally became the right index.

Preorder array index is right subtree flag + 1 and rightIn.

But solving, each time in order to traverse the array index to find the root node, we can begin when the index deposited into the map, when you need direct access, which can reduce the time complexity.

program:

C++

class Solution {
public:
    TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {
        return helper(pre, vin, 0, pre.size()-1, 0, vin.size()-1);
    }
    TreeNode* helper(vector<int>& preorder, vector<int>& inorder, int leftPre, int rightPre, int leftIn, int rightIn){
        if(leftPre > rightPre)
            return nullptr;
        TreeNode* root = new TreeNode(preorder[leftPre]);
        if(leftPre == rightPre)
            return root;
        else{
            int i = leftIn;
            for(;i <= rightIn; ++i){
                if(inorder[i] == preorder[leftPre])
                    break;
            }
            root->left = helper(preorder, inorder, leftPre+1, leftPre+i-leftIn, leftIn, i-1);
            root->right = helper(preorder, inorder, leftPre+i+1-leftIn, rightPre, i+1, rightIn);
            return root;
        }
    }
};

Java

//use HashMap
import java.util.HashMap;
public class Solution {
    public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
        map = new HashMap<>();
        for(int i = 0; i < in.length; ++i){
            map.put(in[i], i);
        }
        return helper(pre, in, 0, pre.length-1, 0, in.length-1);
    }
    public TreeNode helper(int[] preorder, int[] inorder, int leftPre, int rightPre, int leftIn, int rightIn){
        if(leftPre > rightPre)
            return null;
        TreeNode root = new TreeNode(preorder[leftPre]);
        if(leftPre == rightPre)
            return root;
        else{
            int i = map.get(preorder[leftPre]);
            root.left = helper(preorder, inorder, leftPre+1, leftPre+i-leftIn, leftIn, i-1);
            root.right = helper(preorder, inorder, leftPre+i-leftIn+1, rightPre, i+1, rightIn);
            return root;
        }
    }
    private HashMap<Integer, Integer> map;
}

Guess you like

Origin www.cnblogs.com/silentteller/p/11822365.html