博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PTA——03-树3 Tree Traversals Again(25 分)【java语言实现】
阅读量:4569 次
发布时间:2019-06-08

本文共 3164 字,大约阅读时间需要 10 分钟。

03-树3 Tree Traversals Again(25 分)

题目

An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.

Figure 1

Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines follow, each describes a stack operation in the format: “Push X” where X is the index of the node being pushed onto the stack; or “Pop” meaning to pop one node from the stack.

Output Specification:

For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

6Push 1Push 2Push 3PopPopPush 4PopPopPush 5Push 6PopPop

Sample Output:

3 4 2 6 5 1

思路

1) 通过 输入的 push 顺序,得到前序遍历的数组

2)通过 push 和 pop 得到 中序遍历的数组

3)通过前序和中序的数组,可以唯一确定一个后序遍历数组。

代码

import java.util.*;class TreeNode1{    int val;    int left;    int right;}/** * 拥有前序和中序,需要后序遍历结点 */public class Main {
// public static int[] in ={};// int[] pre ={}; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N =sc.nextInt(); // int root =buildTree(sc,N,T1); //list 中第一个为前序,第二个为中序,需要打印的是后序遍历的结果 List
> list = buildTree(sc,N); list.get(0); int[] pre = new int[list.get(0).size()]; int[] in = new int[list.get(1).size()]; for (int i=0;i
res = new ArrayList<>(); post(0,0,in.length-1,in,pre,res); //System.out.println(); for (int i=0;i
> buildTree(Scanner sc, int N){ Stack
stack = new Stack<>();//构建一个堆 List
preOrderTrav = new ArrayList<>(); List
middleOrder = new ArrayList<>(); for (int i=0;i<2*N;i++){ String str = sc.next(); if (str.equals("Push")){ int num = sc.nextInt(); stack.push(num); preOrderTrav.add(num); } else { int num = stack.pop(); middleOrder.add(num); } } List
> res = new ArrayList<>(); res.add(preOrderTrav); res.add(middleOrder); return res; } /** * https://blog.csdn.net/liuchuo/article/details/52135882 * @param root * @param start * @param end * @param in * @param pre * @param res * @return */ public static List
post(int root, int start, int end,int [] in,int []pre,List
res) { if(start > end) return null; int i = start; while(i < end && in[i] != pre[root]) i++; post(root + 1, start, i - 1,in,pre,res); post(root + 1 + i - start, i + 1, end,in,pre,res); res.add(pre[root]); //System.out.println(pre[root]); return res; }}

转载于:https://www.cnblogs.com/HuanChen1025/p/8999275.html

你可能感兴趣的文章
二分——二分查找算法模板
查看>>
关于二次封装css selector 的复数定位
查看>>
自学C#:常用快捷键-随时补充
查看>>
[BZOJ4012] [HNOI2015]开店
查看>>
iOS5新开发的API总述——WWDC 2011
查看>>
JavaScript 基础——使用js的三种方式,js中的变量,js中的输出语句,js中的运算符;js中的分支结构...
查看>>
基于IdentityServer4的OIDC实现单点登录(SSO)原理简析
查看>>
Multicast Routing
查看>>
java NIO中的buffer和channel
查看>>
使用JRegistry来操作window系统注册表
查看>>
函数递归,算法之二分法,表达式,生成式,匿名函数及常用内置函数
查看>>
Nginx,uWSGI与Django 应用的关系
查看>>
Html显示地图
查看>>
MySQL索引选择问题(要相信MySQL自己选择索引的能力)
查看>>
Angular i18n
查看>>
xcode 5.1,引入第三方库,因为第三方库都是自己管理内存,和ARC冲突,需要为部分文件添加Compiler Flags -fno-objc-arc...
查看>>
向数据库中插入一个DateTime类型的数据到一个Date类型的字段中,需要转换类型。TO_DATE('{0}','YYYY-MM-DD'))...
查看>>
【python】关键网站
查看>>
LeetCode 25 —— K 个一组翻转链表
查看>>
Ansible的roles标准化与Jenkins持续集成(三)
查看>>