Classical algorithm problem - the foundation - Reconstruction of Binary Tree

Problem Description

Description Title
input result and the preorder traversal of a binary tree in preorder traversal of the binary tree a rebuild.
Assumed Results preorder traversal order and the input number are no repeating
Example:
preorder traversal sequence {1, 2,4,7,3,5,6,8} and {4,7,2,1,5,3,8,6} sequence preorder
method prototype

public TreeNode reConstructBinaryTree(int [] pre,int [] in)
class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
}

Problem-solving ideas

Hand reduction

Although this problem is a programming problem, but often fill-way trips, such as asking:
Known for a tree:

  • Preorder traversal sequence {1,2,4,7,3,5,6,8}
  • Inorder traversal sequence {4,7,2,1,5,3,8,6}

Seeking the tree postorder traversal . Principle to solve this problem there are two, the first is the root appears pre-order traversing the beginning ; followed by preorder in the left and right sub-tree elements appear in the left and right sides of the root .
The above problem, for example, first-order traversal ago Look, we know the root of the tree is 1, then go to preorder, it is found on the left 1 4,7,2; on the right there is 5,3,8,6. To know the root of the left subtree 4,7,2; and the right subtree 5,3,8,6.

So then how to confirm the shape of the left subtree of it? Just as with the method: to confirm the previous root node traversal sequence ; and confirm the left and right sub-tree in order traversal .
4, 7, 2 sequences, for example, pre-order traversal can view the 2 first appears, therefore 2 is the root node of the subtree , then in order traversal, are found on the left 2 4,7, 2 thus left subtree 4,7 , right subtree is empty, the following structure:

Then, the sub-tree for 4,7, 4 that may be the root node in a preorder traversal of the former 7 is a right side 4 of the child.

Repeated use of the above rules, you can restore the entire tree structure, the following is a complete dynamic presentation:

We restored by the above process to obtain the original shape of the tree:

NATURAL preorder to know Thereafter {7,4,2,5,8,6,3,1}.

Programming Restore

After learning manual restore trees, we will write code to achieve the reconstruction of the tree, which is summarized as previously thought:

  • Confirmed by traversing root preamble
  • Traversing confirm the left and right subtree by inorder
  • The above process recursively, know to restore the leaf nodes

In a specific code implementation, using a recursive algorithm apart, but also using a variable inRootIndex for distinguishing left and right subtree.

The relevant code

package com.shellmad;

public class Solution {

    public static void main(String[] args) {
        int[] pre = {1,2,4,7,3,5,6,8};
        int[] in = {4,7,2,1,5,3,8,6};
        Solution solution = new Solution();
        TreeNode root = solution.reConstructBinaryTree(pre, in);
        System.out.println(root);
    }

    public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
        // 检查输入
        if (pre == null || in == null || pre.length == 0 || in.length == 0
            || pre.length != in.length) {
            return null;
        }

        // 判断是否是叶子节点
        if (pre.length == 1) {
            return new TreeNode(pre[0]);
        }

        // 在中序遍历中找到根节点的位置
        int rootIndex = -1;
        for (int index = 0; index < in.length; index++) {
            if (pre[0] == in[index]) {
                rootIndex = index;
                break;
            }
        }

        if (rootIndex == -1) {
            return null;
        }

        // 创建根节点
        TreeNode root = new TreeNode(pre[0]);

        // 递归构建左子树
        if (rootIndex != 0) {
            int lSubTreeLegth = rootIndex;
            int[] leftPre = new int[lSubTreeLegth];
            int[] leftIn = new int[lSubTreeLegth];

            for (int index = 0; index < lSubTreeLegth; index++) {
                leftPre[index] = pre[1 + index];
                leftIn[index] = in[index];
            }

            root.left = reConstructBinaryTree(leftPre, leftIn);
        }

        // 递归构建右子树
        if (rootIndex != in.length - 1) {
            int rSubTreeLegth = in.length - 1 - rootIndex;
            int[] rightPre = new int[rSubTreeLegth];
            int[] rightIn = new int[rSubTreeLegth];

            for (int index = 0; index < rSubTreeLegth; index++) {
                rightPre[index] = pre[rootIndex + 1 + index];
                rightIn[index] = in[rootIndex + 1 + index];
            }

            root.right = reConstructBinaryTree(rightPre, rightIn);
        }
        return root;
    }
}




class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) { val = x; }
}

Guess you like

Origin www.cnblogs.com/shellmad/p/11706164.html