!求二叉树中从根结点到叶子结点的路径
来源:互联网 发布:哇嘎左下角网络感叹号 编辑:程序博客网 时间:2024/04/27 13:40
/*exp7-3.cpp*/
#include<stdio.h>
#include<malloc.h>
#define MaxSize 100
typedef char ElemType;
typedef struct node
{
ElemType data;/*数据元素*/
struct node *lchild;/*指向左孩子*/
struct node *rchild;/*指向右孩子*/
}BTNode;
extern void CreateBTNode(BTNode * &b,char *str);/*在algo7-1.cpp文件中*/
extern void DispBTNode(BTNode *b);
void AllPath(BTNode *b)/*采用非递归方法输出从根结点到叶子结点的路径*/
{
struct snode
{
BTNode *node;/*存放当前结点指针*/
int parent;/*存放双亲结点在队列中的位置*/
}Qu[MaxSize];/*定义顺序队列*/
int front,rear,p;/*定义队头和队尾指针*/
front=rear=-1;/*置队列为空队列*/
rear++;
Qu[rear].node=b;/*根结点指针进入队列*/
Qu[rear].parent=-1;/*根结点没有双亲结*/
while(front<rear)/*队列不定*/
{
front++;
b=Qu[front].node;/*队头出队列*/
if(b->lchild && b->rchild==NULL)/*b为叶子结点*/
{
printf("%c到根结点路径:",b->data);
p=front;
while(Qu[p].parent!=-1)
{
printf(" %c",Qu[p].node->data);
p=Qu[p].parent;
}
printf("%c\n",Qu[p].node->data);
}
if(b->lchild!=NULL)/*左孩子入队列*/
{
rear++;
Qu[rear].node=b->lchild;
Qu[rear].parent=front;
}
if(b->rchild!=NULL)/*右孩子入队列*/
{
rear++;
Qu[rear].node=b->rchild;
Qu[rear].parent=front;
}
}
}
void AllPath1(BTNode *b,ElemType path[],int pathlen)/*采用递归方法输出从根结点到叶子结点的路径*/
{
int i;
if(b!=NULL)
{
if(b->lchild==NULL && b->rchild==NULL)/**b为叶子结点*/
{
printf("%c到根结点路径:%c",b->data,b->data);
for(i=pathlen-1;i>=0;i--)
printf("%c",path[i]);
printf("\n");
}
else
{
path[pathlen]=b->data;/*将当前结点放入路径当中*/
pathlen++;/*路径长度增加1*/
AllPath1(b->lchild,path,pathlen);/*递归扫描左子树*/
AllPath1(b->rchild,path,pathlen);/*递归扫描右子树*/
pathlen--;/*恢复环境*/
}
}
}
void LongPath(BTNode *b,ElemType path[],int pathlen,ElemType longpath[],int &longpathlen)
{
int i;
if(b==NULL)
{
if(pathlen>longpathlen)/*若当前路径更长,将路径保存在longpath中*/
{
for(i=pathlen-1;i>=0;i--)
longpath[i]=path[i];
longpathlen=pathlen;
}
}
else
{
path[pathlen]=b->data;/*将当前结点放入路径中*/
pathlen++;/*路径长度增1*/
LongPath(b->lchild,path,pathlen,longpath,longpathlen);/*递归扫描左子树*/
LongPath(b->rchild,path,pathlen,longpath,longpathlen);/*递归扫描右子树*/
pathlen--;/*恢复环境*/
}
}
void DispLeaf(BTNode *b)/*输出叶子结点*/
{
if(b!=NULL)
{
if(b->rchild==NULL && b->lchild==NULL)
printf("%c",b->data);
else
{
DispLeaf(b->lchild);
DispLeaf(b->rchild);
}
}
}
void main()
{
BTNode *b;
ElemType path[MaxSize],longpath[MaxSize];
int i,longpathlen=0;
CreateBTNode(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))");
printf("二叉树b: ");DispBTNode(b);printf("\n\n");
printf("b的叶子结点:");DispLeaf(b);printf("\n\n");
printf("AllPath:\n");AllPath(b);printf("\n");
printf("AllPath1:\n");AllPath1(b,path,0);printf("\n");
LongPath(b,path,0,longpath,longpathlen);
printf("第一条最长路径长度:%d\n",longpathlen);
printf("第一条最长路径:");
for(i=longpathlen;i>=0;i--)
printf(" %c",longpath[i]);
printf("\n");
}
- !求二叉树中从根结点到叶子结点的路径
- 输出二叉树中所有从根结点到叶子结点的路径
- 输出二叉树中所有从根结点到叶子结点的路径
- 输出二叉树中所有从根结点到叶子结点的路径
- 二叉树中从根结点到叶子结点的所有路径(递归实现)
- 二叉树中从根结点到叶子结点所有路径(非递归实现)
- 【31】给定一个二叉树打印出所有从根结点到叶子结点路径和为 k 的路径
- 【31】给定一个二叉树打印出所有从根结点到叶子结点路径和为 k 的路径
- 打印从根结点到叶子结点的路径(递归)
- c++输出二叉树叶子结点并输出叶子结点到根结点的路径长度
- java 遍历树结点 同时保留所有从根到叶子结点的路径
- 打印二叉树根结点到所有叶子结点的路径
- 求二叉树的叶子结点数
- POJ 2499 求二叉树结点到根结点的路径长度 递归 二叉树
- 根结点到所有叶子结点的路径问题
- 输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
- 输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
- 给定一个二叉树,节点值为0-9,从根节点到叶子结点组成一个数,求二叉树所有组成的数的和
- teechart
- 按装一个新的apk并恢复到刚烧机时的操作步骤
- 多进程访问r/w配置文件(.ini格式)时,总是发生文件被破坏的情况
- post方式发送数据
- Java.toString()
- !求二叉树中从根结点到叶子结点的路径
- 2013“加博会”开启帮助企业拓展国内市场的渠道
- Windows 不能在本地计算机启动 IIS Admin Service
- 判断是否重复登录
- float与double的范围和精度
- IOS内存泄露
- 整合Struts2、Spring、Hibernate构建J2EE应用
- strcspn函数
- 网站架构(页面静态化,图片服务器分离,负载均衡)方案全解析