根据前序和中序列 重建二叉树


输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并输出它的后序遍历序列。

输入:

输入可能包含多个测试样例,对于每个测试案例,

输入的第一行为一个整数n(1<=n<=1000):代表二叉树的节点个数。

输入的第二行包括n个整数(其中每个元素a的范围为(1<=a<=1000)):代表二叉树的前序遍历序列。

输入的第三行包括n个整数(其中每个元素a的范围为(1<=a<=1000)):代表二叉树的中序遍历序列。

输出:

对应每个测试案例,输出一行:

如果题目中所给的前序和中序遍历序列能构成一棵二叉树,则输出n个整数,代表二叉树的后序遍历序列,每个元素后面都有空格。

如果题目中所给的前序和中序遍历序列不能构成一棵二叉树,则输出”No”。

样例输入:
81 2 4 7 3 5 6 84 7 2 1 5 3 8 681 2 4 7 3 5 6 84 1 2 7 5 3 8 6
样例输出:
7 4 2 5 8 6 3 1 No
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <vector>
#include <math.h>

using namespace std;

struct node{
	node *left;
	node *right;
	int val;
	node()
	{
		right=NULL;
		left=NULL;
		val=0;
	};
} ;
node *creat(int preorder[],int len,int inorder[])
{
	if(len<=0)
	return NULL;
	node *root=NULL;
	int val=preorder[0];
	int i=0;
	for(;i<len;i++)
	{
		if(inorder[i]==val)
		{
			break;
		}
	}
	if(i>=len)
	{
		return NULL;
	}
	else
	{
		root=new node();
		root->val=val;
		root->left=creat(&preorder[1],i,inorder);
		root->right=creat(&preorder[i+1],len-i-1,&inorder[i+1]);
		return root;
	}
	return NULL;
}
void preorder(node *root)
{
	if(root)
	{
		cout<<root->val<<endl;
		preorder(root->left);
		preorder(root->right);
	}
}
int main()
{ 
 int a[8]={1,2,4,7,3,5,6,8};
 int b[8]={4,7,2,1,5,3,8,6};
 node *root=creat(a,8,b);
 preorder(root);   
}


根据前序和中序列 重建二叉树

时间: 2024-08-29 10:47:23

根据前序和中序列 重建二叉树的相关文章

依据前序和中序列 重建二叉树

输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.如果输入的前序遍历和中序遍历的结果中都不含反复的数字.比如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6}.则重建二叉树并输出它的后序遍历序列. 输入: 输入可能包括多个測试例子.对于每一个測试案例, 输入的第一行为一个整数n(1<=n<=1000):代表二叉树的节点个数. 输入的第二行包含n个整数(当中每一个元素a的范围为(1<=a<=1000)):代表二叉树的前序遍历序列

根据前序和中序重建二叉树

注意:1.仅根据前序和后序无法构建唯一的二叉树:2.二叉树前序遍历,第一个数字总是树的根节点的值:3.中序遍历中,根节点的值在序列的中间,左子树的值子在根节点的值的左边,右字树的值在根节点的值的右边:4.思路:递归 #include <iostream> #include <stdlib.h> using namespace std; struct Node{ int value; Node* left; Node* right; }; Node* ConstructCore(in

OpenJudge 由中根顺序和后根序列重建二叉树

[题解] 后根序列的最后一个元素即为二叉树的树根root.root将中根序列分为两部分,左半边是左子树的中根序列,而右半边则是右部分的中根序列.同时后根序列依照左子树和右子树节点数也可以被分为左子树的后根序列和右子树的后根序列.于是便可依此递归地按左右子树的后根.中根序列重建子树,最终重建二叉树. [代码] 1 #include <iostream> 2 using namespace std; 3 /*build BT from its inorder and postorder*/ 4 5

前序和中序构造二叉树

题目链接: 涉及知识:二叉树的遍历 分析: 二叉树的前序遍历:根节点 -> 左子树 -> 右子树 二叉树的中序遍历:左子树 -> 根节点 -> 右子树 由此可知:前序遍历中访问到的第一个元素便是根节点,通过该点便可以将中序遍历分成左右两部分,左部分的元素用来生成该二叉树的左子树,右部分用来生成二叉树的右子树. 同样,左右两部分的元素中,首先在前序遍历中出现的便是该子树的根节点,很明显符合递归的定义. 代码如下: /* * @lc app=leetcode.cn id=105 lan

Tree Recovery(由先、中序列构建二叉树)

题目来源: http://poj.org/problem?id=2255 题目描述: Description Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes. This is an example of one of her cr

二叉树遍历 A1086.Tree Traversals Again(25) (转化为给定前序和中序列,求后序)

#include <bits/stdc++.h> #include <stdio.h> #include <stdlib.h> #include <queue> using namespace std; const int maxn = 50; struct node{ int data; node* lchild; node* rchild; }; int pre[maxn],in[maxn],post[maxn]; int n; node* create

由先序和中序重建二叉树

思路: 我们知道,前序遍历的第一个节点就是树的根节点,所以我们先根据前序遍历序列的第一个数字创建根结点,接下来在中序遍历序列中找到根结点的位置,根节点的左边就是左子树,右边就是右子树,这样就能确定左.右子树结点的数量.在前序遍历和中序遍历的序列中划分了左.右子树结点的值之后,就可以递归地去分别构建它的左右子树. /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode

题目:1385 由前序和中序构建二叉树

http://ac.jobdu.com/problem.php?pid=1385 蛮怀旧的题目,记得大一就见过一直没做过,没难度,纯小心吧. 类似的是有中序和后续构建二叉树.比如http://www.cnblogs.com/kaituorensheng/p/3788533.html 思路很简单 递归构造: #include <cstdio> #include <cstring> #include <iostream> #include <cstdio> #i

C++根据前序和中序构造二叉树

#include <iostream> #include <string.h> using namespace std; template<typename Type> struct Node { Type data; Node<Type> *left; Node<Type> *right; Node(Type d = Type()):data(d),left(NULL),right(NULL){} }; template<typename