给出一棵二叉树的先序和中序数组,通过这两个数组直接生成正确的后序数组。

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;


// 思路,利用先序遍历和中序遍历将树构建出来,再用后续遍历遍历树,放在数组中,再输入结果
// ps: 直接输出也可以,写注释的时候才想到可以直接输出
// 先序遍历第一个一定是根节点
// 中序遍历表示着遍历的顺序,将中序遍历的值作为Key,在数组中的位置作为value
// map.get(Key1) > map.get(Key2) 的话,说明Key2比Key1先遍历到
// 也就是key2在key1左边

public class Main{
    
    // 先序遍历数组
    public static Integer [] pre;
    // 中序遍历数组
    public static Integer [] mid;
    // 后续遍历数组
    public static Integer [] result;
    
    // 存放中序遍历的值,和它所在位置的map
    public static Map<Integer, Integer> map;
    
    // 根节点
    public static Node root;
    
    // 用于控制向后续遍历数组存放结果位置的值
    public static int index;
    
    // 表示左子树
    public static int L = 1;
    // 表示右子树
    public static int R = 2;
    
    
    // 初始化,为了接受数据,非必要,手动构造数据可以自己初始化
    // 本题输入数据格式
    // 3
    // 1 2 3
    // 2 1 3  

    public static void init(){
        Scanner scan = new Scanner(System.in);
        scan.nextLine();
        String[] preS = scan.nextLine().split(" ");
        String[] midS = scan.nextLine().split(" ");
        
        pre = new Integer[preS.length];
        mid = new Integer[midS.length];
        result = new Integer[preS.length];
        root = null;
        index = 0;
        map = new HashMap<>();
        for(int i=0; i<preS.length; i++){
            pre[i] = Integer.valueOf(preS[i]);
            mid[i] = Integer.valueOf(midS[i]);
            map.put(mid[i], i);
        }
        scan.close();
    }
    
    
    // 构建树
    public static void buildTree(){
        // 根节点一定是先序一个元素
        root = new Node(null, null, pre[0]);
        Node point = root;
        Node preNode = root;
        int direction = L;
        
        for(int i=1; i<pre.length; i++){
            // 记录当前节点
            point = root;
            // 记录当前节点的上一个节点
            preNode = root;
            direction = L;
            
            while(point != null){
                
                // point向左子树移动
                if(map.get(point.value) > map.get(pre[i])){
                    preNode = point;
                    point = point.left;
                    direction = L;
                }
                
                // point向右子树移动
                else{
                    preNode = point;
                    point = point.right;
                    direction = R;
                }
            }
            
            
            if(direction == L)
                preNode.left = new Node(null, null, pre[i]);
            else if(direction == R)
                preNode.right = new Node(null, null, pre[i]);
            
        }
       
        
    }
    
    // 懒得写非递归了,数据量不大,写了个递归后续便利
    public static void putIntoResult(Node root) {
        if(root != null) {
            putIntoResult(root.left);
            putIntoResult(root.right);
            result[index] = root.value;
            index ++;
        }
    }
    
    
    // 展示数据
    public static void show() {
         for(int i=0; i<result.length; i++) {
             if(i+1 == result.length)
                 System.out.print(result[i]);
             else
                 System.out.print(result[i] + " ");
         }
    }
    
    
    public static void main(String[] args) {
        Main.init();
        Main.buildTree();
        Main.buildTree();    
        Main.putIntoResult(root);
        Main.show();
    }
    
    
}

// 节点类
class Node{
    public Node left;
    public Node right;
    public Integer value;
    
    public Node(Node left, Node right, Integer value){
        this.left = left;
        this.right = right;
        this.value = value;
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值