`
风吹过PP好冷
  • 浏览: 36651 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

PAT1020 Tree Traversals

    博客分类:
  • PAT
阅读更多

已知中序遍历 后序遍历,求层次遍历

 

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

 

 

#include <string.h>
#include <iostream>
using namespace std;
#define MAX_TREE_SIZE 30
typedef struct BiTNode 
{    
	int data;    
	struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree; 

BiTree CreateBT(int *postorder,int *inorder,int n)
{    
	BiTree bt;    
	int *p;    
	int k;    
	if (n<=0)     
	{        
		return NULL;    
	}    
	bt=(BiTree)malloc(sizeof(BiTNode));    
	bt->data = *(postorder);    
	for(p = inorder; p < inorder + n; p++)    
	{        
		if (*p == *postorder)
		{            
			break;        
		}    
	}    
	k = p-inorder;    
	bt->lchild = CreateBT(postorder-n+k,inorder,k);    
	bt->rchild = CreateBT(postorder-1,p+1,n-k-1);    
	return bt;
} 


void LevelOrder(BiTree b, int K)
{
	int Tree[MAX_TREE_SIZE] = {0};
	int i = 0;
	BiTree p;
	BiTree qu[MAX_TREE_SIZE];   //定义环形队列,存放结点指针
	int front = -1, rear = -1;	//定义队头和队尾指针
	rear++;
	qu[rear] = b;				//根结点指针进入队列
	while (front != rear)		//队列不为空
	{
		front = (front+1)%MAX_TREE_SIZE;
		p = qu[front];			//队头出队列
		Tree[i++] = p->data;	//访问结点
		if (p->lchild != NULL)	//有左孩子时将其进队
		{ 
			rear = (rear+1)%MAX_TREE_SIZE;
			qu[rear] = p->lchild;
		}
		if (p->rchild != NULL)	//有右孩子时将其进队
		{ 
			rear = (rear+1)%MAX_TREE_SIZE;
			qu[rear] = p->rchild;
		}
	}

	for (int j = 0; j < K - 1; j++)
	{
		cout<<Tree[j]<<" ";
	}
	cout<<Tree[K - 1];
}

int main()
{    
	BiTree T;    
	int Tree1[MAX_TREE_SIZE] = {0};
	int Tree2[MAX_TREE_SIZE] = {0}; 
	int K;
	cin>>K;
	for (int i = 0; i < K; i++)
	{
		int x;
		cin>>x;
		Tree1[i] = x;
	}
	for (int i = 0; i < K; i++)
	{
		int x;
		cin>>x;
		Tree2[i] = x;
	}
	T = CreateBT(Tree1 + K - 1, Tree2 , K);    
	LevelOrder(T,K);
	return 0;
}
 
分享到:
评论

相关推荐

    03-树2. Tree Traversals Again.zip

    给你inorder的栈操作步骤,让你写出postorder后的序列。 http://blog.csdn.net/xkzju2010/article/details/46325457

    Heyinsen#LeetcodeOrPat#1020 Tree Traversals (25 分) 后序和中序构建树1

    思路后序和中序构造二叉树唯一一个注意的地方,在注释中。// 注意这里是pos-right_num-1而不是想当然的pos_inr-1,这两个的位置并不一定对应。

    陈越、何钦铭-数据结构作业11:Tree Traversals Again二叉树非递归遍历/栈遍历

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

    7-1 Tree Traversals Again_DS_pta_C++_AgainAgain_

    PTA 7-1 Tree Traversals Again

    c(数据结构).

    c (数据结构) AVLTree Graph Heap Tree Traversals Again 计算器_栈 二叉排序树 二分法查找 线性表 等 值得学习

    BinaryTree-traversals:React Application呈现二叉树遍历

    cd BinaryTree遍历 npm安装 npm开始 浏览到htpp:// localhost:3000 登陆页面 包含一棵静态树。 用户可以选择任何遍历(顺序,前顺序或后顺序),遍历的结果将显示在树下。 您也可以同时签出算法。 预购遍历 后...

    Binary_tree_order_traversals

    Binary_tree_order_traversals 在该项目中,我们可以看到树的顺序,前顺序和后顺序。 该程序将打开一个菜单,并允许用户通过一个接一个地导入数据(节点)来创建他/她的树: '''0。退出 插入 展示 搜索 为了 预购...

    leetcode焦虑-Interview-Notes:采访笔记

    Tree traversals, search Stack Queue Graph traversals(BFS, DFS) 最短路径数学题 从之前的采访中吸取的错误和教训: 调度: 更加注意时间安排,在公司现场面试前保持2-3天的开放时间。 除非非常关键,否则不要立即...

    FileOutputBuffer.rar_Cause

    In debug build this will cause full traversals of the tree when the validate is called on insert and remove. Useful for debugging but very slow.

    Programming Interview Problems and Algorithms in Ruby

    Tree Serialization, Finding the Top k Elements of Data Streams, MapReduce, Partial Sorting, the Skyline Problem, DFS, BFS and Topological Sorting of Dags, the Alternative Alphabet and the Phone Words...

    Data Structure and Algorithms

    3.7 Tree Traversals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 3.7.1 Preorder . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 I 3.7.2 Postorder . . . . . . . . . . . . . . ....

    BFS.zip_bfs_in

    Breadth first search algorithm in C. Example are countries in Romania traversals.

    开源项目-mdempsky-unbed.zip

    开源项目-mdempsky-unbed.zip,unbed: refactoring tool to remove implicit field traversals

    PURE-FTPD for WINDOWS 源码

    Before all : Pure-FTPd was designed on Unix and for Unix. The Windows port has been done because some people are forced to work on Win32 by their ...traversals, device opening, etc) .

    算法:经典算法和最知名算法的实现

    This repository contains various classic algorithms about ordenation, graph traversals, dynamic programing and some crypto related stuff as well. 在这个仓库中: - Peak finding (1D/2D) - Binary ...

    Neo4j High Performance(PACKT,2015)

    Study the in-built graph algorithms for better traversals and discover Spring-Data-Neo4j Look under the hood of Neo4j, covering concepts from the core classes in the source to the internal storage ...

    Android代码-janusgraph

    can support thousands of concurrent users, complex traversals, and analytic graph queries. Learn More The project homepage contains more information on JanusGraph and provides links to ...

    Android代码-probe

    Dissect layout traversals on Android. Features Intercept View methods. onMeasure(int, int) onLayout(boolean, int, int, int, int) draw(Canvas) and onDraw(Canvas) requestLayout() Override any of ...

    Data Structures & Algorithms in Swift 4th Edition

    * Traversals: Traverse graphs and trees efficiently with breadth-first, depth-first, Dijkstra’s and Prim’s algorithms to solve problems such as finding the shortest path or lowest cost in a network...

    世界_天气_分析

    conda4.7.12 资料来源1: : 数据源2: : 软体:Jupyter Notebook,Pandas Library,CityPy,Python Request,API,JSON Traversals概括第一部分:获取每个城市的天气描述和降水量笔记本:Weater_Database.ipynb ...

Global site tag (gtag.js) - Google Analytics