6. rebuild binary tree

6. rebuild 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.

Problem-solving ideas: Recursive

Ideas: to understand the characteristics before and after the sequence preorder traversal, the sequence preorder traversal of the first element behind the root node of the left subtree + all all right subtree nodes; root sequence preorder left are left subtree node on the right are the right sub-tree node

  1. Root node is found by a preorder traversal sequence val
  2. By valand find the sequence preorder left subtree sequence length len, in order to obtain a sequence of left subtree motif sequence and right subtree
  3. By lenbefore getting left subtree order traversal sequence and a preamble sequence motif sequences right subtree
  4. Repeat step 123, the root node each return a termination condition len == 0, or by the length of the subtree 0

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-VNde9KeO-1570784357816) (/ images / wins the offer / 6. Reconstruction binary .png)]

  • Every time acquire the entire contents of the new pre-order traversal and inorder traversal to recursively
/* function TreeNode(x) {
    this.val = x;
    this.left = null;
    this.right = null;
} */
function reConstructBinaryTree(pre, vin)
{
    // write code here
    if (!pre|| !vin || pre.length == 0 || vin.length == 0)
        return null;
    return binaryTreeCore(pre, 0, pre.length - 1, vin, 0, vin.length - 1);
}

function binaryTreeCore(pre, preStart, preEnd, vin, vinStart, vinEnd) {
    var rootVal = pre[preStart];
    var rootIndex = vin.indexOf(rootVal);
    var treeNode = new TreeNode(rootVal);
    if (vinStart < rootIndex) treeNode.left = binaryTreeCore(pre, preStart + 1, rootIndex - vinStart + preStart, vin, vinStart, rootIndex - 1);
    if (rootIndex < vinEnd) treeNode.right = binaryTreeCore(pre, rootIndex - vinStart + preStart + 1, preEnd, vin, rootIndex + 1, vinEnd);
    return treeNode;
}
  • With each sequence in the sequence to the root node and recursively
/**
 * 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)
            return null;
        return binaryTree(pre, in, 0, 0, in.length - 1);
    }

    public TreeNode binaryTree(int[] pre, int[] in, int rootIndex, int inStart, int inEnd) {
        int rootVal = pre[rootIndex];
        TreeNode treeNode = new TreeNode(rootVal);
        for (int i = inStart; i <= inEnd; i++) {
            if (in[i] == rootVal) {
                                                                     //注意不要使用 ++rootIndex,因为下一步会用到rootIndex
                if (i > inStart) treeNode.left = binaryTree(pre, in, rootIndex + 1, inStart, i - 1);
                if (i < inEnd) treeNode.right = binaryTree(pre, in, i - inStart + rootIndex + 1, i + 1, inEnd);
            }
        }
        return treeNode;
    }
}

Published 241 original articles · won praise 14 · views 50000 +

Guess you like

Origin blog.csdn.net/qq_29150765/article/details/102504803