HDOJ 题目1710 Binary Tree Traversals(二叉搜索树)

Binary Tree Traversals

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3912    Accepted Submission(s): 1741

Problem Description

A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most important ways in which the vertices of a binary tree can be systematically traversed
or ordered. They are preorder, inorder and postorder. Let T be a binary tree with root r and subtrees T1,T2.

In a preorder traversal of the vertices of T, we visit the root r followed by visiting the vertices of T1 in preorder, then the vertices of T2 in preorder.

In an inorder traversal of the vertices of T, we visit the vertices of T1 in inorder, then the root r, followed by the vertices of T2 in inorder.

In a postorder traversal of the vertices of T, we visit the vertices of T1 in postorder, then the vertices of T2 in postorder and finally we visit r.

Now you are given the preorder sequence and inorder sequence of a certain binary tree. Try to find out its postorder sequence.

Input

The input contains several test cases. The first line of each test case contains a single integer n (1<=n<=1000), the number of vertices of the binary tree. Followed by two lines, respectively indicating the preorder sequence and inorder sequence. You can assume
they are always correspond to a exclusive binary tree.

Output

For each test case print a single line specifying the corresponding postorder sequence.

Sample Input

9
1 2 4 7 3 5 8 9 6
4 7 2 1 8 5 9 3 6

Sample Output

7 4 2 8 9 5 6 3 1

Source

HDU 2007-Spring Programming Contest

Recommend

lcy   |   We have carefully selected several similar problems for you:  1707 1512 1714 1175 1044

中序和前序变后序遍历

ac代码

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct s
{
	struct s *l,*r;
	int num;
}node,*Node;
Node root;
int n;
int a[3000],b[3000];
Node creat(int *a,int *b,int n)
{
	Node tree;
	for(int i=0;i<n;i++)
	{
		if(a[0]==b[i])
		{
			tree=(Node)calloc(1,sizeof(node));
			tree->num=b[i];
			tree->l=creat(a+1,b,i);
			tree->r=creat(a+i+1,b+i+1,n-i-1);
			return tree;
		}
	}
	return NULL;
}
void houxu(Node cur)
{
	//Node cur=root;
	if(cur!=NULL)
	{
		houxu(cur->l);
		houxu(cur->r);
		if(cur==root)
			printf("%d",cur->num);
		else
			printf("%d ",cur->num);
	}
}
int main()
{
	//Node root;
	root=(Node)calloc(1,sizeof(node));
	while(scanf("%d",&n)!=EOF)
	{
		int i;
		for(i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
		}
		for(i=0;i<n;i++)
			scanf("%d",&b[i]);
		root=creat(a,b,n);
		houxu(root);
		printf("\n");
	}
	//while(1);
}
时间: 2024-10-11 11:39:27

HDOJ 题目1710 Binary Tree Traversals(二叉搜索树)的相关文章

[LeetCode] 255. Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree. You may assume each number in the sequence is unique. Follow up:Could you do it using only constant space complexity? 给一个数组,验证是否为一个二叉搜索树的

hdu 1710 Binary Tree Traversals 前序遍历和中序推后序

题链;http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4205    Accepted Submission(s): 1904 Problem Description A binary tree i

[CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树

4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原理,请参见我之前的博客Validate Binary Search Tree 验证二叉搜索树.

[LeetCode] Recover Binary Search Tree 复原二叉搜索树

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? confused what "{1,#,2,3}"

[LeetCode] Validate Binary Search Tree 验证二叉搜索树

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

[LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1) time and uses

669. Trim a Binary Search Tree 修剪二叉搜索树

Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R](R >= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary

173 Binary Search Tree Iterator 二叉搜索树迭代器

实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器.调用 next() 将返回二叉搜索树中的下一个最小的数.注意: next() 和hasNext() 操作的时间复杂度是O(1),并使用 O(h) 内存,其中 h 是树的高度. 详见:https://leetcode.com/problems/binary-search-tree-iterator/description/ /** * Definition for binary tree * struct TreeNode { *

[LeetCode] 99. Recover Binary Search Tree 复原二叉搜索树

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Example 1: Input: [1,3,null,null,2]   1   /  3     2 Output: [3,1,null,null,2]   3   /  1     2 Example 2: Input: [3,1,4,null,null,2]