Rebuild binary tree

Question
Know the preorder and inorder traversal sequence of a binary tree, this binary tree has no duplicate data

Problem- solving ideas
1) Knowing the preorder and inorder sequence of a binary tree can uniquely determine a binary tree
2) Recursion, dividing the array into segments Points that
write picture description here
should be
noted 1) The given sequence cannot contain repeated numbers
2) Give The specified sequence must be the correct sequence.
Test case
Oblique binary tree, ordinary binary tree, empty binary tree
Solution
There are two solutions encountered.
1) It is more common, passing array references

/**
 * 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) {
       if(pre == null || in == null || pre.length == 0 || in.length == 0 || (pre.length != in.length)) {
           return null;
       }
       TreeNode root = createBinaryTree(pre, 0, pre.length - 1, in, 0, in.length - 1);
       return root;
    }

    public TreeNode createBinaryTree(int [] pre, int preSta, int preEnd, int [] in, int inSta, int inEnd) {
        if(preSta > preEnd || inSta > inEnd) {
            return null;
        }
        TreeNode treeNode = new TreeNode(pre[preSta]);
        int i = 0;
        for(; i < in.length; i++) {
            if(pre[preSta] == in[i]) {
                break;
            }
        }
        treeNode.left = createBinaryTree(pre, preSta + 1, preSta + i - inSta, in, inSta, i - 1);
        treeNode.right = createBinaryTree(pre, preSta + i - inSta+1, preEnd, in, i + 1, inEnd);
        return treeNode;
    }
}

Another solution is to use Arrays.copyOfArray(arr orignal, int from, int to);
the characteristics of this function
1) It is written in native methods, not in java code, but written in C
2) [from, to) ;
3) Regardless of whether there are elements copied or not, an array reference will be obtained

public static void main(String[] args) {        
        int[] a = {1};
        int[] b = Arrays.copyOfRange(a, 1, 1);
        if(b == null) {
            System.out.println("b is null");
        } else {
            // 执行这个分支 输出 0
            System.out.println(b.length);
        }   
    }

The following method consumes more memory, but it cannot be compiled in Niuke.

链接:https://www.nowcoder.com/questionTerminal/8a19cbe657394eeaac2f6ea9b0f6fcf6
来源:牛客网

public class Solution {
    public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
       if(pre.length == 0||in.length == 0){
            return null;
        }
        TreeNode node = new TreeNode(pre[0]);
        for(int i = 0; i < in.length; i++){
            if(pre[0] == in[i]){
                node.left = reConstructBinaryTree(Arrays.copyOfRange(pre, 1, i+1), Arrays.copyOfRange(in, 0, i));
                node.right = reConstructBinaryTree(Arrays.copyOfRange(pre, i+1, pre.length), Arrays.copyOfRange(in, i+1,in.length));
                break;
            }
        }
        return node;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324594022&siteId=291194637