(Java)1020 Tree Traversals分数 25

文章讲解了如何利用后序和中序遍历构造二叉树,然后通过层次遍历算法求得给定序列对应的树的级序遍历结果。

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

假设二叉树中的所有键都是不同的正整数。给定后序和序遍历序列,您应该输出相应二叉树的级序遍历序列。

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

每个输入文件包含一个测试用例。对于每种情况,第一行给出一个正整数N(≤30),即二叉树中的节点总数。第二行给出了后序序列,第三行给出了有序序列。一行中的所有数字都用空格隔开。

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

对于每个测试用例,在一行中打印相应二叉树的级别顺序遍历序列。一行中的所有数字必须正好用一个空格分隔,并且行的末尾不能有多余的空格。

Sample Input:

7
2 3 1 5 7 6 4
1 2 3 4 5 6 7

Sample Output:

4 1 6 3 5 7 2

思路:我们可以通过中序遍历 + 后序遍历构造出一颗唯一的二叉树(前序+后序 不能构造出一颗唯一的二叉树!!),然后通过层序遍历得到答案,中序 + 后序遍历 构造出二叉树 可以参考一下leetcode 106题

从中序与后序遍历序列构造二叉树

import java.util.*;
class Main{
    static int[]postorder ;
    static int[]inorder;
    static int n;
    static Map<Integer,Integer> map = new HashMap<>();
    static int post_index;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        postorder = new int[n];
        inorder = new int[n];
        for(int i = 0 ; i < n ;i++)
        {
            postorder[i] = sc.nextInt();

        }
        for(int i = 0 ; i < n ;i++)
        {
            inorder[i] = sc.nextInt();
        }
        post_index = n - 1;
        int index = 0;
        for(int k :inorder)
        {
            map.put(inorder[index] , index++);
        }
        TreeNode root = BuilderTree(0,n-1);

        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        ArrayList<Integer> arr = new ArrayList<>();
        while(!q.isEmpty())
        {
            int n = q.size();
            for(int i = 0 ; i < n ;i++)
            {
                TreeNode temp = q.poll();
                arr.add(temp.val);
                if(temp.left != null) q.offer(temp.left);
                if(temp.right != null) q.offer(temp.right);
            }
        }
        for(int i = 0 ; i < arr.size() ;i++)
        {
            if(i == 0) System.out.print(arr.get(i));
            else System.out.print(" " + arr.get(i));
        }
    }

    private static TreeNode BuilderTree(int left, int right) {
        if(left > right) return null;
        int num = postorder[post_index];
        post_index --;
        TreeNode root = new TreeNode(num);
        int index = map.get(num);
        root.right = BuilderTree(index + 1,right);
        root.left = BuilderTree(left ,index - 1);
        return root;
    }
}
class TreeNode{
    int val;
    TreeNode left,right;
    public TreeNode()
    {

    }
    public TreeNode(int num)
    {
        this.val = num;
    }
}

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值