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, ...
随机推荐
- Ligerui Grid组件--学生信息列表
一.目录 1.多层架构+MVC+EF+AUTOFAC+AUTOMAPPER: 2.MVC中验证码的实现(经常用,记录备用) 3.Ligerui首页的快速搭建 4.Ligerui Grid组件--学生信 ...
- Asp.net 同时下载多个文件
整理自网络 下载思路是首先把多个文件进行压缩,然后再下载压缩成的压缩包 引用文件dll:ICSharpCode.SharpZipLib.dll 1. 合成下载文件夹 Protected Sub btn ...
- Unix无缓冲文件操作函数、文件信息查询
问题描述: Unix无缓冲文件操作函数.文件信息查询 问题解决: struct stat 结构体信息: 具体代码: 具体源文件:
- 【转】System.DateTime.Now.ToString()的一些用法
C#中的日期处理函数 //2007年4月24日 this.TextBox6.Text = System.DateTime.Now.ToString("D"); ...
- A const field of a reference type other than string can only be initialized with null Error [duplicate]
I'm trying to create a 2D array to store some values that don't change like this. const int[,] hiveI ...
- Proxmox虚拟机增加硬盘容量
1.首先在虚拟机控制台选择调整硬盘容量,弹出窗口为增加的容量 2.重启虚拟机,用fdisk –l查看新增容量是否被识别 3.用cfdisk创建分区,分区格式为Primary 8e (Linux LVM ...
- Create a method synchronized without using synchronized keyword
Actually, lots of ways: No need for synchronization at all if you don't have mutable state. No need ...
- POJ 3080 Blue Jeans (多个字符串的最长公共序列,暴力比较)
题意:给出m个字符串,找出其中的最长公共子序列,如果相同长度的有多个,输出按字母排序中的第一个. 思路:数据小,因此枚举第一个字符串的所有子字符串s,再一个个比较,是否为其它字符串的字串.判断是否为字 ...
- java基础知识回顾之java Thread类学习(四)--java多线程安全问题(锁)
上一节售票系统中我们发现,打印出了错票,0,-1,出现了多线程安全问题.我们分析为什么会发生多线程安全问题? 看下面线程的主要代码: @Override public void run() { // ...
- 【转】Android 实现“再按一次退出程序”
From:http://blog.csdn.net/ldj299/article/details/7574365 个人觉得当用户按下后退键时,出现"再按一次退出"的提示防止误操作比 ...