数据结构实验之二叉树七:叶子问题
2015-11-27 21:28
204 查看
#include <stdio.h> #include <stdlib.h> #include <queue> #include <iostream> using namespace std; char s[101]; struct Node { char data; struct Node * lchild,* rchild; }; struct Node * root; int cnt; //构造二叉树 struct Node * Build_tree() { struct Node * root; if(s[cnt++] == ',') root = NULL; else { root = (struct Node *)malloc(sizeof(struct Node)); root -> data = s[cnt - 1]; root -> lchild = Build_tree(); root -> rchild = Build_tree(); } return root; } void yeziwenti(Node *root) //叶子节点 { queue<Node*>s; s.push(root); while(!s.empty()) { root = s.front(); s.pop(); if(root) { if((root->lchild==NULL) && (root->rchild==NULL)) printf("%c", root->data); s.push(root->lchild); s.push(root->rchild); } } } int main() { int n; while(scanf("%s",s)!=EOF) { cnt = 0; root = Build_tree(); //建树 yeziwenti(root); cout<<endl; } return 0; }
相关文章推荐
- 数据结构实验之二叉树三:统计叶子数
- 数据结构实验 第一单元 集合交并
- 数据结构实验 第一单元 集合交并
- 数据结构实验 第一单元 线性表之链表操作
- 数据结构实验 第一单元 线性表之链表操作
- 数据结构基础6.5:拓扑排序TopOrder
- 数据结构实验之图论八:欧拉回路
- 数据结构实验之图论七:驴友计划
- 数据结构基础6.4:最短路径(Dijkstra, Floyd)
- 数据结构上机【构造哈夫曼树,并输出对应节点的哈夫曼编码】
- 数据结构实验之图论六:村村通公路
- 数据结构上机 【创建二叉树,并采用先中后序遍历,输出树高,度数为分别为0 1 2 的结点个数】
- 数据结构实验之图论二:基于邻接表的广度优先搜索遍历
- 数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历
- getchar,getch,scanf,gets,getche一群纠结的兄弟
- [数据结构JAVA版]集合
- 数据结构之内存---对和栈的区别
- 数据结构实验之图论七:驴友计划 最短路中最小花费
- 贪婪算法+小应用(调度问题)
- 《大话数据结构》之Kruskal算法