POJ 1577 Falling Leaves
题意:给出一些字符串,从上到下的建树,输出其前序遍历
像前面那一题一样,先建树,然后再递归前序遍历
不过想像上一题那样用数组建树,建树和上题一样的办法,可是应该怎么输出前序遍历呢= =
还是看的题解= =
#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的更多相关文章
- POJ 1577 Falling Leaves 二叉搜索树
HDU 3791 Falling Leaves 二叉搜索树 Figure 1Figure 1 shows a graphical representation of a binary tree of ...
- 【二叉搜索树】poj 1577 Falling Leaves
http://poj.org/problem?id=1577 [题意] 有一颗二叉搜索树,每次操作都把二叉搜索树的叶子从左到右揪掉(露出来的父节点就变成了新的叶子结点) 先给出了揪掉的叶子序列(多个字 ...
- POJ 1577 Falling Leaves (子母二叉树,给出叶子节点的删除序列,求前序遍历)
题意:给出一棵字母二叉树删除叶子节点的序列,按删除的顺序排列.让你输出该棵二叉树额前序遍历的序列.思路:先把一棵树的所有删除的叶子节点序列存储下来,然后从最后一行字符串开始建树即可,最后遍历输出. ...
- POJ 1577 Falling Leaves(二叉搜索树)
思路:当时学长讲了之后,似乎有点思路----------就是倒着建一个 二叉搜索树 代码1:超时 详见超时原因 #include<iostream> #include<cstrin ...
- H - The Falling Leaves
Description Each year, fall in the North Central region is accompanied by the brilliant colors of th ...
- UVa699 The Falling Leaves
// UVa699 The Falling Leaves // 题意:给一棵二叉树,每个节点都有一个水平位置:左儿子在它左边1个单位,右儿子在右边1个单位.从左向右输出每个水平位置的所有结点的权值 ...
- UVA - 699The Falling Leaves(递归先序二叉树)
The Falling Leaves Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Sub ...
- UVA.699 The Falling Leaves (二叉树 思维题)
UVA.699 The Falling Leaves (二叉树 思维题) 题意分析 理解题意花了好半天,其实就是求建完树后再一条竖线上的所有节点的权值之和,如果按照普通的建树然后在计算的方法,是不方便 ...
- 【数据结构】The Falling Leaves(6-10)
[UVA699]The Falling Leaves 算法入门经典第6章例题6-10(P159) 题目大意:有一颗二叉树,求水平位置的和. 试题分析:乱搞就可以过,将树根节点的pos记为0,向左-1, ...
随机推荐
- WinForm程序界面假死,寻求完美解决方案
故事的开端是这样的,小白是一个程序员,他确实也是一个小白,目前还在程序员发展的道路上,兢兢业业的小心求学. 有一天,小白接到一个任务,完成一个Winform程序,附加一个功能就是可以读IC卡. 小白终 ...
- 【环境】Linux下连接无线网常用命令
启用/重启/关闭 网络服务 /etc/init.d/networking start /etc/init.d/networking restart /etc/init.d/networking sto ...
- "Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b7
1.错误背景 系统安装了.net framework4.0.4.5,项目先使用VS2013(4.5)开发,后来又重新用VS2010开发(4.0),运行时出现这个错误 2.错误原因 In .Net 4. ...
- NYOJ-79 拦截导弹 AC 分类: NYOJ 2014-01-01 23:25 167人阅读 评论(0) 收藏
#include<stdio.h> int main(){ int num[1000]={0}; int n,m,x,y; scanf("%d",&n); wh ...
- Finite State Machine
Contents [hide] 1 Description 2 Components 3 C# - FSMSystem.cs 4 Example Description This is a Dete ...
- linux源码阅读笔记 数组定义
在阅读linux源码的过程中遇到了下面的略显奇怪的结构体数组定义. static struct hd_struct{ long start_sect; long nr_sects; }hd[10]={ ...
- 解谜谷歌 DevOps:什么特质可以打造世界级可靠系统?
[编者按]本文是 Gene Kim 总结自对 Randy Shoup 两个小时的采访,主要关注谷歌 DevOps 的提升之道.本文系 OneAPM联合高效运维编译整理. Randy Shoup 曾协助 ...
- hdu 1879 继续畅通工程(最小生成树,基础)
题目 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<algo ...
- struts2学习笔记(2)——简单的输入验证以及标签库的运用
struts2自带了一些标签库,运用好这些标签库会减少开发周期. 1.struts2标签库是在哪定义的? struts2标签库定义在struts2-core-2.1.8.jar这个文件中,具体在这个j ...
- Android 近百个项目的源代码
Android 近百个项目的源代码 Android PDF 阅读器 http://sourceforge.net/projects/andpdf/files/个人记账工具 OnMyMeans http ...