题意:给出一些字符串,从上到下的建树,输出其前序遍历

像前面那一题一样,先建树,然后再递归前序遍历

不过想像上一题那样用数组建树,建树和上题一样的办法,可是应该怎么输出前序遍历呢= =

还是看的题解= =

 #include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<algorithm>
using namespace std; typedef long long LL;
const int maxn=;
char str[maxn][maxn]; typedef struct node{
char v;
node *l,*r;
} node; node* newnode(char ch){ // 创建一个新的节点
node* u=(node*)malloc(sizeof(node));
if(u!=NULL){
u->v=ch;
u->l=u->r=NULL;
}
return u;
} node* addnode(char ch,node* nd){ // 增加节点
if(nd==NULL) nd=newnode(ch);
else{
if(ch>=nd->v) nd->r=addnode(ch,nd->r);//如果更大, 放在当前这个头的右子树
else nd->l=addnode(ch,nd->l);//如果更小,放在 当前这个头的左子树
}
return nd;
} void dfs(node* nd){
if(nd!=NULL){
printf("%c",nd->v);
dfs(nd->l);
dfs(nd->r);
}
} int main()
{
int i,j,cnt=;
node *root=NULL;
while(scanf("%s",str[cnt])){
char tmp=str[cnt][];
if(tmp=='*'||tmp=='$'){
for(i=cnt-;i>=;i--)
for(j=;j<strlen(str[i]);j++)
root=addnode(str[i][j],root); dfs(root);
printf("\n");
root=NULL;
cnt=;
memset(str,,sizeof(str));
}
else
cnt++;
if(tmp=='$') break;
}
return ;
}

话说做了几题二叉树= =还是不会建树啊啊啊----

POJ 1577 Falling Leaves的更多相关文章

  1. POJ 1577 Falling Leaves 二叉搜索树

    HDU 3791 Falling Leaves 二叉搜索树  Figure 1Figure 1 shows a graphical representation of a binary tree of ...

  2. 【二叉搜索树】poj 1577 Falling Leaves

    http://poj.org/problem?id=1577 [题意] 有一颗二叉搜索树,每次操作都把二叉搜索树的叶子从左到右揪掉(露出来的父节点就变成了新的叶子结点) 先给出了揪掉的叶子序列(多个字 ...

  3. POJ 1577 Falling Leaves (子母二叉树,给出叶子节点的删除序列,求前序遍历)

    题意:给出一棵字母二叉树删除叶子节点的序列,按删除的顺序排列.让你输出该棵二叉树额前序遍历的序列.思路:先把一棵树的所有删除的叶子节点序列存储下来,然后从最后一行字符串开始建树即可,最后遍历输出.   ...

  4. POJ 1577 Falling Leaves(二叉搜索树)

    思路:当时学长讲了之后,似乎有点思路----------就是倒着建一个  二叉搜索树 代码1:超时 详见超时原因 #include<iostream> #include<cstrin ...

  5. H - The Falling Leaves

    Description Each year, fall in the North Central region is accompanied by the brilliant colors of th ...

  6. UVa699 The Falling Leaves

      // UVa699 The Falling Leaves // 题意:给一棵二叉树,每个节点都有一个水平位置:左儿子在它左边1个单位,右儿子在右边1个单位.从左向右输出每个水平位置的所有结点的权值 ...

  7. UVA - 699The Falling Leaves(递归先序二叉树)

    The Falling Leaves Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Sub ...

  8. UVA.699 The Falling Leaves (二叉树 思维题)

    UVA.699 The Falling Leaves (二叉树 思维题) 题意分析 理解题意花了好半天,其实就是求建完树后再一条竖线上的所有节点的权值之和,如果按照普通的建树然后在计算的方法,是不方便 ...

  9. 【数据结构】The Falling Leaves(6-10)

    [UVA699]The Falling Leaves 算法入门经典第6章例题6-10(P159) 题目大意:有一颗二叉树,求水平位置的和. 试题分析:乱搞就可以过,将树根节点的pos记为0,向左-1, ...

随机推荐

  1. Azure VM 远程无法登陆问题(No Remote Desktop License)

    解决方法: 打开 Wins+R=> mstsc /v: yourVMIPadress /admin mstsc /v:xx.xx.xx.xx:54738 /admin

  2. InnoDB与UUID

    CakePHP本身有一个uuid实现,所以一直以来,我都在尝试使用uuid做主键的可能性.虽然MySQL是我最常用的数据库,但是和 auto_increment_int主键相比,我对uuid主键更有好 ...

  3. Codeforces Round #327 (Div. 2) E. Three States

    题目链接: 题目 E. Three States time limit per test:5 seconds memory limit per test:512 megabytes 问题描述 The ...

  4. 【转】Tarjan&LCA题集

    转自:http://blog.csdn.net/shahdza/article/details/7779356 [HDU][强连通]:1269 迷宫城堡 判断是否是一个强连通★2767Proving ...

  5. 【BZOJ】【2190】【SDOI2008】仪仗队

    欧拉函数/莫比乌斯函数 Orz iwtwiioi 这个嘛……很明显在同一条线上的两个点一定是满足  x1*k=x2,y1*k=y2,(好吧这个表示方式有点傻逼,懂得就好了)那么这条线上的点只有第一个会 ...

  6. sampler2d

    Here is the syntax for a sampler in Direct3D 9. sampler Name = SamplerType{   Texture = <texture_ ...

  7. VS2010 创建WindowsService服务

    1.新建一个Windows 服务 2.添加Installer 这一步很重要,在处理完你的业务逻辑后需要添加一个Installer才能是你的Windows服务被安装. 在VS中添加Installer 右 ...

  8. Appboy 基于 MongoDB 的数据密集型实践

    摘要:Appboy 正在过手机等新兴渠道尝试一种新的方法,让机构可以与顾客建立更好的关系,可以说是市场自动化产业的一个前沿探索者.在移动端探索上,该公司已经取得了一定的成功,知名产品有 iHeartM ...

  9. POJ 3579

    Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3528   Accepted: 1001 Descriptio ...

  10. [转]SQL Server建立应用程序安全性和程序角色

    转自:http://dev.yesky.com/450/7619450.shtml 2007-10-22 14:00 来源:论坛整理 作者:luolina 责任编辑:幽灵·yesky Microsof ...