您的位置:首页 > 理论基础 > 数据结构算法

数据结构实验之二叉树七:叶子问题

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: