HDU-1710 给出前序中序输出后序
来源:互联网 发布:木托盘设计软件 编辑:程序博客网 时间:2024/05/29 08:45
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.
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.
91 2 4 7 3 5 8 9 64 7 2 1 8 5 9 3 6
Sample Output
7 4 2 8 9 5 6 3 1
给出中序和前序,输出后序,(这种题得变形可以给出后序和中序,要有中序来确定根节点)
前序是 先根历遍的,所以前序的第一个是根。然后这个根把中序分为两半,左边是左子树,右边是右子树。然后递归
#include<stdio.h>#include<stdlib.h>#include <stack>#include <iostream>using namespace std;typedef struct Tree{ Tree *left; Tree *right; int value;}Tree,*t;Tree *root;Tree* create(int *preorder,int *inorder,int n){ Tree *temp; for(int i=0;i<n;i++) { if(preorder[0]==inorder[i])//前者是根,如果在中序找到这个根 { temp=(Tree*)malloc(sizeof(Tree));//开辟新结点 temp->value=inorder[i];//该结点的值就是找到的当前根的值 temp->left=create(preorder+1,inorder,i);//通过+1把前序序列已经找过的结点移出去,因为是左子树,所以中序指针不变 temp->right=create(preorder+i+1,inorder+i+1,n-i-1);//右子树,所以下一个结点在之前找到的中序结点的右边 return temp; } } return NULL;}void postOrder(Tree *postTree){ /*if(postTree!=NULL) { postOrder(postTree->left); postOrder(postTree->right); if(postTree==root)//找到最后结束,换行,指针值相比较 printf("%d\n",postTree->value); else printf("%d ",postTree->value); }*/ stack <t> stack;//非递归 Tree *p=postTree; do { while(p!=NULL) { stack.push(p); p=p->left; } bool flag=true; Tree *r=NULL; while(!stack.empty()&&flag) { p=stack.top(); if(p->right==r) { if(p==root) printf("%d\n",p->value); else printf("%d ",p->value); stack.pop(); r=p; } else { p=p->right; flag=false; } } }while(!stack.empty());}int main(){ int n; int preorder[2010],inorder[2010]; while(scanf("%d",&n)!=EOF) { root=NULL; for(int i=0;i<n;i++) scanf("%d",&preorder[i]);//输入前序 for(int i=0;i<n;i++) scanf("%d",&inorder[i]);//输入中序 root=create(preorder,inorder,n);//从前序和中序得到二叉树 postOrder(root);//得到后序遍历序列 } return 0;}图析:
//看思想:btree *bulid(btree *pre,btree *in,int n)//给出前序和中递归建立二叉树{ btree *p,*b; int k; p=(btree *)malloc(btree); p->date=pre->date; for(b=in;b<in+n;++b) if(b->date==p->date) break; k=b-in;//k代表根结点值的那个位置,在下次递归判断的时候判断到k之前的那个节点就可以了 p->lchild=build(pre+1,in,k); p->rchild=build(pre+k+1,p+1,n-k);//是n-k(因为下次要从前序得第k+1个位置开始进行判断) return p; }btree *build(btree *post,btree *in,int n)//后序,中序建立树{ btree *p,*b; int k; p=(btree *)malloc(sizeof(btree)); p->date=(post+n-1)->date; for(b=in;b<in+n;++b) if(b->date==p->date) break; k=b-in; p->lchild=build(post,in,k); p->rchild=build(post+k+1,b+1,n-k-1); //后续:左子树 右子树 根节点 中续:左子树 根节点 右子树 return b;}
阅读全文
1 0
- HDU-1710 给出前序中序输出后序
- 给出前序和中序遍历,输出后序遍历
- 已知前序中序,输出后序
- 给出 中序&后序 序列 建树;给出 先序&中序 序列 建树
- 【Java实现】给出两个整型数组,将他们合并后从小到大排序并输出
- 给出中序和后序遍历,构造二叉树以及给出前序和中序构造二叉树
- 输入10个整数,将它们从小到大排序后输出,并给出现在每个元素在原来序列中的位置。
- 给出年月日输出下一天的日期
- 给出日期输出是星期几
- 给出一百分制成绩,要求输出成绩
- 模板-根据前序中序序列输出后序序列(杭电1710)
- 给出二叉树的先序和中序遍历,递归求解后序遍历
- 为什么只给出前序以及后序遍历,不能生成唯一的二叉树
- 为什么只给出前序和后序,不能唯一确定一个二叉树
- 只给出先序遍历和后序遍历不能唯一确定二叉树的例子
- 判断给出的序列是不是一个二叉搜索树的后序遍历
- 二叉树遍历:已知前序中序输出后序/已知后序中序输出前序
- 根据二叉树先序中序序列输出后序
- 一个项目
- 【OpenCV入门教程之十二】OpenCV边缘检测:Canny算子,Sobel算子,Laplace算子,Scharr滤波器合辑
- ThreadPoolExecutor
- JS 日期的获取和计算 ios不兼容问题
- 2017.10.28 排序 思考记录
- HDU-1710 给出前序中序输出后序
- css揭秘-2
- jsp基本介绍
- 算法分析与设计第八周
- Python学习(5)--列表
- C++面试题之 main函数执行完之后还会调用其他的函数吗?
- JAVA进阶之Servlet、JSP
- lua学习笔记—table
- Android 退出app,循环遍历退出