Reconstruction of binary prove safety offer (illustration)

Topic Source: prove safety net offer thematic cattle off
topic Address: reconstruction of binary tree

Title Description

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.

Knowledge base

You know what is a binary tree, the preamble sequence binary tree traversal sequence, and how to determine a unique binary tree?

Binary tree : each node has at most two subtrees (i.e., left and right subtree subtrees) of a tree structure.
Preorder traversal (about root): root access first, then turn to traverse the left subtree and right subtree.
Preorder (root left and right): first traverse the left subtree, then visit the root node, and then traverse the right subtree.
Postorder (about root): Traverse the left subtree first, then traverse the right subtree, last accessed the root node.
How to determine the unique binary : known preorder sequence and the preamble / sequence preorder traversal .

If your knowledge of binary tree is vague, rush to replenish learn about: in-depth study binary tree (a) the basis of a binary tree

Reference thinking

1. The sequence preorder traversal to determine the root root = pre [startPre].
2. Locate the position of the root node i in the sequence preorder the left and right of the root node is the left and right subtrees in order traversal sequence.
The junction sequence preorder number of the left subtree determined point (i-startIn) left subtree, whereby the left and right subtrees sequence preorder traversal.
4. The left and right subtrees using the preamble sequence preorder and repeat steps 1-3, a recursive binary tree, and finally returns the root to root.
Note: When startPre> endPre || startIn> returns null endIn.
Binary Tree root of graphic process ,
Here Insert Picture Description
the root graphical process left subtree ,
Here Insert Picture Description
the root of the right subtree of graphic process ,
Here Insert Picture Description
the reconstruction of a binary tree ,
Here Insert Picture Description

Reference Code

public class Solution {
    public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
        TreeNode root=buildTree(pre,0,pre.length-1,in,0,in.length-1);
        return root;
    }
    public TreeNode buildTree(int []pre,int startPre,int endPre,int []in,int startIn,int endIn){
        if(startPre>endPre||startIn>endIn){
            return null;
        }
        //前序遍历先访问根结点,所以前序遍历序列的首个元素即为根结点。
        TreeNode root=new TreeNode(pre[startPre]);
        //在中序遍历序列中寻找根节点的位置,并将序列分为左右子树
        for(int i=startIn;i<=endIn;i++){
            if(in[i]==pre[startPre]){
                int leftcount=i-startIn;//求左子树结点个数
                //在左子树序列中寻找根的左孩子结点
                root.left = buildTree(pre,startPre+1,startPre+leftcount,in,startIn,i-1);
                //在右子树序列中寻找根的右孩子结点
                root.right = buildTree(pre,startPre+leftcount+1,endPre,in,i+1,endIn);
                break;
            }
        }
        return root;
    }
}
Published 18 original articles · won praise 40 · views 50000 +

Guess you like

Origin blog.csdn.net/seawaysyyy/article/details/105369773