POJ 1577 Falling Leaves

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

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

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

还是看的题解= =

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include <cmath>
 5 #include<algorithm>
 6 using namespace std;
 7
 8 typedef long long LL;
 9 const int maxn=105;
10 char str[maxn][maxn];
11
12 typedef struct node{
13     char v;
14     node *l,*r;
15 } node;
16
17 node* newnode(char ch){   // 创建一个新的节点
18     node* u=(node*)malloc(sizeof(node));
19     if(u!=NULL){
20         u->v=ch;
21     u->l=u->r=NULL;
22     }
23     return u;
24 }
25
26 node* addnode(char ch,node* nd){ // 增加节点
27     if(nd==NULL) nd=newnode(ch);
28     else{
29         if(ch>=nd->v) nd->r=addnode(ch,nd->r);//如果更大, 放在当前这个头的右子树
30         else nd->l=addnode(ch,nd->l);//如果更小,放在 当前这个头的左子树
31     }
32     return nd;
33 }
34
35 void dfs(node* nd){
36     if(nd!=NULL){
37         printf("%c",nd->v);
38         dfs(nd->l);
39         dfs(nd->r);
40     }
41 }
42
43 int main()
44 {
45     int i,j,cnt=0;
46     node *root=NULL;
47     while(scanf("%s",str[cnt])){
48         char tmp=str[cnt][0];
49         if(tmp==‘*‘||tmp==‘$‘){
50             for(i=cnt-1;i>=0;i--)
51                 for(j=0;j<strlen(str[i]);j++)
52                 root=addnode(str[i][j],root);
53
54                 dfs(root);
55                 printf("\n");
56                 root=NULL;
57                 cnt=0;
58                 memset(str,0,sizeof(str));
59         }
60         else
61         cnt++;
62         if(tmp==‘$‘) break;
63     }
64     return 0;
65 }

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

时间: 2024-08-03 23:06:17

POJ 1577 Falling Leaves的相关文章

POJ 1577 Falling Leaves 二叉树题解

给出按最底层叶子节点到根节点的数据,然后要求重建树,前序输出最终建的树. 都是两个基本操作解决: 1 二叉树插入操作 2 前序遍历 简单题目了. #include <stdio.h> #include <string> #include <algorithm> #include <vector> using std::vector; using std::string; const int MAX_B = 1024; char buf[MAX_B]; int

POJ 1577 Falling Leaves 二叉搜索树

HDU 3791 Falling Leaves 二叉搜索树  Figure 1Figure 1 shows a graphical representation of a binary tree of letters. People familiar with binary trees can skip over the definitions of a binary tree of letters, leaves of a binary tree, and a binary search tr

POJ 1577 Falling Leaves 二叉树操作

Description Figure 1 Figure 1 shows a graphical representation of a binary tree of letters. People familiar with binary trees can skip over the definitions of a binary tree of letters, leaves of a binary tree, and a binary search tree of letters, and

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

[UVA699]The Falling Leaves 算法入门经典第6章例题6-10(P159) 题目大意:有一颗二叉树,求水平位置的和. 试题分析:乱搞就可以过,将树根节点的pos记为0,向左-1,向右+1,统计答案即可. #include<iostream> #include<cstring> #include<cstdio> #include<vector> #include<queue> #include<stack> #in

UVa 699.The Falling Leaves【7月23】

The Falling Leaves Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on the trees, followed quickly by the falling leaves accumulating under the trees. If the same thing happened to binary trees, how lar

UVA 699 The Falling Leaves

#include<cstdio> #include<iostream> #include<queue> #include<cstring> #include<string> #include<math.h> #include<stack> #include<cstdlib> #include<map> #include<algorithm> #include<cctype>

H - The Falling Leaves

Description Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on the trees, followed quickly by the falling leaves accumulating under the trees. If the same thing happened to binary trees, how large woul

UVA 699 The Falling Leaves (二叉树水题)

本文纯属原创,转载请注明出处,谢谢.http://blog.csdn.net/zip_fan. Description Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on the trees, followed quickly by the falling leaves accumulating under the trees. If the sam

UVa 699 The Falling Leaves(递归建树)

题意  假设一棵二叉树也会落叶  而且叶子只会垂直下落   每个节点保存的值为那个节点上的叶子数   求所有叶子全部下落后   地面从左到右每堆有多少片叶子 和上一题有点像  都是递归输入的  一个节点(设水平位置为p)  则它的左右儿子节点的水平位置分别为  p-1  p+1   也是可以边输入边处理的  输入完也就得到答案了   注意每个样例后面都有一个空行  包括最后一个 #include<cstdio> #include<cstring> using namespace s