To prove safety offer face questions 7 (java version): reconstruction of a binary tree

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/littlehaes/article/details/91383163

welcome to my blog

To prove safety offer face questions 7 (java version): reconstruction of a 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.

Thinking

  • Paint is not easy to think out of bounds
  • Experience recursive thought
  • The title parameter passed in the recursion, the constant array is preorder traversal sequence and the first two, he said change is a value representative of the subtree starting index and ending index

the complexity

Time complexity: continuous half, the time complexity is O (logn)
Complexity Space: no use of additional memory space, the space complexity is O (1)

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
        // 1.健壮性判断
        if(pre.length != in.length || pre.length <=0 || in.length <=0 )
            return null;
        // 2.开始正常执行
        TreeNode root = reConstructBinaryTree(pre, 0, pre.length-1, in, 0, in.length-1);
        return root;
    }
    public TreeNode reConstructBinaryTree(int[] pre, int preStart, int preEnd, int[] in, int inStart, int inEnd){
        /*
        1. 在前序遍历中找到根节点
        2. 在中序遍历中找到根节点
        3. 找出左子树(注意判断左子树是否存在)及其对应的前序遍历和中序遍历结果
        4. 找出右子树(注意判断右子树是否存在)及其对应的前序遍历和中序遍历结果
        */
        //1.
        TreeNode root = new TreeNode(pre[preStart]);
        //2.
        int leftLen=0;
        for(int i=inStart; i<= inEnd; i++){
            if(in[i] == pre[preStart])
                break;
            leftLen++;
        }
        //3.
        if(leftLen>0){
            int preStartLeft = preStart+1;
            int preEndLeft = preStart+leftLen;
            int inStartLeft = inStart;
            int inEndLeft = inStart + leftLen -1;
            root.left = reConstructBinaryTree(pre, preStartLeft, preEndLeft, in, inStartLeft, inEndLeft);
        }
        if((preEnd - preStart) - leftLen > 0 ){
            int preStartRight = preStart+leftLen+1;
            int preEndRight = preEnd;
            int inStartRight = inStart+leftLen+1;
            int inEndRight = inEnd;
            root.right = reConstructBinaryTree(pre, preStartRight, preEndRight, in, inStartRight, inEndRight);
        }
        return root;
    }
}

Guess you like

Origin blog.csdn.net/littlehaes/article/details/91383163