04. Rebuilding Binary Tree

topic

Enter the results of the preorder traversal and midorder traversal of a binary tree, please reconstruct the binary tree. Suppose that the input pre-order traversal and mid-order traversal results do not contain duplicate numbers. For example, input the pre-order traversal sequence {1,2,4,7,3,5,6,8} and mid-order traversal sequence {4,7,2,1,5,3,8,6}, then reconstruct the binary tree return.

analysis

The binary tree can be determined according to middle-order traversal and pre-order traversal. The specific process is:

  1. Determine the root node (first node) according to the preamble
  2. Split into left and right subtrees according to the position of the root node in the middle sequence
  3. Recurse the left and right subtrees separately, using the same method to decompose

Pre-order traversal:
left and right Root traversal: left root right
Post-order traversal: left and right root

achieve

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
import java.util.Arrays;
public class Solution {
    public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
        if(pre.length == 0 || in.length == 0){
            return null;
        }
        //根节点放入TreeNode中
        TreeNode treeNode = new TreeNode(pre[0]);
        //遍历中序遍历的数组 得到根节点下面的左子树和右子树
        for(int i = 0; i < in.length; i++){
            if(in[i] == pre[0]){
                //左子树递归
                treeNode.left = reConstructBinaryTree(Arrays.copyOfRange(pre, 1, i+1), Arrays.copyOfRange(in, 0, i));
                //右子树递归
                treeNode.right = reConstructBinaryTree(Arrays.copyOfRange(pre, i+1, pre.length), Arrays.copyOfRange(in, i + 1, in.length));
                //break;不用break,到了叶子节点,两边都是null,可以自动返回
            }
        }
        return treeNode;
    }
}

Note the copyOfRange method in the Arrays tool class, the interval is closed from left to right:

public static int [] copyOfRange (int [] original, int from, int to)
copies the specified range of the specified array to a new array. The initial index (from) of the range must be between 0 and original.length (inclusive). The value at original [from] is placed in the initial element of the copy (unless from == original.length or from == to). The values ​​of subsequent elements in the original array are placed in the subsequent elements of the copy. The last index (to) of the range (must be greater than or equal to from) can be greater than original.length, in which case 0 is placed in all elements of the copy with index greater than or equal to original.length-from The length of the returned array is to-from.
Parameters:
original-an array
from which to copy a range from-the initial index of the range to be copied (inclusive)
to-> the last index of the range to be copied (not included). (This index can be outside the range of the array).

Published 12 original articles · praised 0 · visits 89

Guess you like

Origin blog.csdn.net/charlotte_tsui/article/details/105327048