根据先序中序构建二叉树

输入二叉树的前序遍历和中序遍历,求后序遍历或者重构二叉树

   例如:前序:1,2,4,7,3,5,6,8
        中序:4,7,2,1,5,3,6,8

        后序:7,4,2,5,8,6,3,1

  方法1:如果需要重构再输出后序
   步骤如下:
       1.根据前序确定根节点就是第一个;
       2.根据根节点在中序的位置确定左右子树
      3.递归重复上述1 2 步骤
      4.二叉树重构完成
       5.再递归后序遍历二叉树


       如此递归

树结点
package com.banban.CeShi.BinaryTree;

/**
 * @author :zhangpengzhan
 * @date :Created in 2019/4/2 19:54
 * @name :BinaryTreeNode
 */
public class BinaryTreeNode {
    private int value;
    private BinaryTreeNode leftNode;
    private BinaryTreeNode rightNode;

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public BinaryTreeNode getLeftNode() {
        return leftNode;
    }

    public void setLeftNode(BinaryTreeNode leftNode) {
        this.leftNode = leftNode;
    }

    public BinaryTreeNode getRightNode() {
        return rightNode;
    }

    public void setRightNode(BinaryTreeNode rightNode) {
        this.rightNode = rightNode;
    }

    @Override
    public String toString() {
        return "BinaryTreeNode{" +
                "value=" + value +
                '}';
    }
}

算法:
package com.banban.CeShi.BinaryTree;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author :zhangpengzhan
 * @date :Created in 2019/4/2 19:41
 * @name :ReBuildTree
 *
 *  输入二叉树的前序遍历和中序遍历,求后序遍历或者重构二叉树
 *
 *      例如:前序:1,2,4,7,3,5,6,8
 *           中序:4,7,2,1,5,3,6,8
 *
 *           后序:7,4,2,5,8,6,3,1
 *
 *      方法1:如果需要重构再输出后序
 *      步骤如下:
 *          1.根据前序确定根节点就是第一个;
 *          2.根据根节点在中序的位置确定左右子树
 *          3.递归重复上述1 2 步骤
 *          4.二叉树重构完成
 *          5.再递归后序遍历二叉树

 *
 */
public class ReBuildTree {
    //private static ArrayList arrayList = new ArrayList<>();



    public static BinaryTreeNode buildTree(int[] pre,int[] ord){
        if (pre.length == 0 ||ord.length == 0){
            return null;
        }
        BinaryTreeNode treeNode = new BinaryTreeNode();
        treeNode.setValue(pre[0]);
        for (int i = 0; i <ord.length ; i++) {
            if (ord[i]==pre[0]){
                treeNode.setLeftNode(buildTree(Arrays.copyOfRange(pre,1,i+1),Arrays.copyOfRange(ord,0,i)));
                treeNode.setRightNode(buildTree(Arrays.copyOfRange(pre,i+1,pre.length),Arrays.copyOfRange(ord,i+1,ord.length)));
            }
        }
        return treeNode;
    }
    public static void postTree(BinaryTreeNode treeNode){
        if (treeNode != null){
            postTree(treeNode.getLeftNode());
            postTree(treeNode.getRightNode());
            //arrayList.add(treeNode.getValue());
            System.out.println(treeNode.getValue());
        }
    }


//    public static void postCover(int[] pre, int[] ord,int[] post,int index){
//        for (int i = 0; i 
//            if (pre[0] == ord[i]){
//                    post[index] = ord[i];
//                    //前面
//                    postCover(Arrays.copyOfRange(pre,1,i+1),Arrays.copyOfRange(ord,0,i),post,i-1);
//                    //后面
//                    postCover(Arrays.copyOfRange(pre,i+1,pre.length), Arrays.copyOfRange(ord,i+1,ord.length),post,index-1);
//
//            }
//        }
//    }



    public static void main(String[] args) {
        int[] pre = new int[]{1,2,4,7,3,5,6,8};
        int[] ord = new int[]{4,7,2,1,5,3,8,6};
        int[] post = new int[8];

        BinaryTreeNode tree = buildTree(pre, ord);
        postTree(tree);


    }
}

你可能感兴趣的:(每日一算法)