求二叉树排序树后序,http://livearchive.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_pro

15 篇文章 0 订阅
要给这个水题ORZ。。。。。。。我真的是弱爆了,这个烂题耽误我快一下午。。。。 哭我他妈的弱爆了。。。各种跪烂。。。上午TC的一题各种跪。。。。下午又是各种ORZ。。就是给出二叉排序树的先序遍历,求后序,,,,,对先序排下序就有中序了,,,各种OTL,,,其实直接建树就行。。。然后直接后序。。。
#include<cstdio>
//#include<stdlib.h>
#include<algorithm>
#include<vector>
using namespace std;
 struct node
 {
   int data;
   node *lc,*rc;
   node(int x)
   {
     data=x;lc=rc=NULL;
   }
 };
 void build(node *t,int v)
 {
	 if(t->data>v)
	 {
		 if(t->lc==NULL)
		 {
			 t->lc=new node(v);
		 }
		 else
			 build(t->lc,v);
	 }
	 else
	 {
	    if(t->rc==NULL)
		{
		  t->rc=new node(v);
		}
		else
			build(t->rc,v);
	 }
 }
 void post(node *t)
 { 
   if(t!=NULL)
   { 
     post(t->lc);
	 post(t->rc);
	 printf("%d\n",t->data);
   }
 }
int main()
{
	  /*freopen("1.txt","r",stdin);
	freopen("2.txt","w",stdout); */ 
	int x;
	   
	scanf("%d",&x);
	node tm(x);
	  
	while(~scanf("%d",&x))
	{   build(&tm,x);
	}
	 post(&tm);
	 
 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值