pat1020. Tree Traversals (25)

算法思路:

1、后序最后元素为根,根将中序分为左右子树

2、层序遍历利用队列实现,java使用LinkedList

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

class Node{
	int key;
	Node left;
	Node right;
	public Node(int key){
		this.key=key;
	}
}
public class Main {
	static int num;
	static int post[];
	static int in[];
	public static Node createBT(int postH, int postT, int inH, int inT){
		if(postH>=postT)return null;
		int key=post[postT-1];
		Node root=new Node(key);
		int i=inH;
		for(;i<inT;i++)
			if(key==in[i])break;
		root.left=createBT(postH,i-inH+postH,inH,i);
		root.right=createBT(i-inH+postH,postT-1,i+1,inT);
		return root;
	}
	public static void levelTr(Node root){
		Queue<Node> que=new LinkedList<Node>();
		if(null!=root)System.out.print(root.key);
		if(null!=root.left)que.offer(root.left);
		if(null!=root.right)que.offer(root.right);
		while(!que.isEmpty()){
			Node node=que.poll();
			System.out.print(" "+node.key);
			if(null!=node.left)que.offer(node.left);
			if(null!=node.right)que.offer(node.right);
		}
	}
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		num=sc.nextInt();
		post=new int[num];
		in=new int[num];
		for(int i=0;i<num;i++)
			post[i]=sc.nextInt();
		for(int i=0;i<num;i++)
			in[i]=sc.nextInt();
		Node root=createBT(0,num,0,num);
		levelTr(root);
		sc.close();
	}
}

时间: 2024-10-28 06:36:20

pat1020. Tree Traversals (25)的相关文章

PAT 1020. Tree Traversals (25)

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.

【PAT】1020 Tree Traversals (25)(25 分)

1020 Tree Traversals (25)(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

PAT Advanced 1020 Tree Traversals (25分)

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.

1020. Tree Traversals (25) PAT甲级真题

之前我看了这道题,实在是看不懂网上的解题答案,他们的具体思路基本上就是通过后续遍历和中序遍历,直接推出层次遍历. 我苦思冥想了半天,是在没看懂这种思路,于是想了一个笨点的但是也比较好理解的思路,通过后续和中序,先推出整个二叉树,再考虑 对二叉树层次遍历. 本题还有一点要注意的时在输出结果的末尾,如果使用了类似 pirntf("%d ",data); 这样的格式是不对的,一定要对末尾进行判断消除最尾端的空格. 首先最核心的部分是通过两次遍历反推回二叉树:这里的思路是,后续遍历的最末尾,一

A1020. 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

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

1020. Tree Traversals (25)

根据两种遍历方式建立二叉树 层遍历二叉树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 le

1020. Tree Traversals (25) -BFS

题目如下: 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:

浙大pat甲级题目---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