【数据结构_树_Tree_0972】统计利用先序计算二叉树宽度
2017-03-16 22:00
260 查看
A##
ABC####
AB##C##
ABCD###E#F##G##
A##B##
-------------------------------------
1
3
2
4
1
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
typedef struct node
{
char data;
struct node *L_Kid,*R_Kid;
}Tree;
int a[1005]={0};
void initTree(Tree *&T)
{
char str;
cin>>str;
if(str!='#')
{
T=(Tree *)malloc(sizeof(Tree));
T->data=str;
initTree(T->L_Kid);
initTree(T->R_Kid);
}
else T=NULL;
}
void width(Tree *&T,int l)
{
if(T!=NULL)
{
a[l]++;
width(T->L_Kid,l+1);
width(T->R_Kid,l+1);
}
}
int main()
{
Tree *T;
initTree(T);
int l=1;
width(T,l);
int max=a[0];
for(int i=0;i<100;i++)
{
if(a[i]>max) max=a[i];
}
cout<<max;
return 0;
}
ABC####
AB##C##
ABCD###E#F##G##
A##B##
-------------------------------------
1
3
2
4
1
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
typedef struct node
{
char data;
struct node *L_Kid,*R_Kid;
}Tree;
int a[1005]={0};
void initTree(Tree *&T)
{
char str;
cin>>str;
if(str!='#')
{
T=(Tree *)malloc(sizeof(Tree));
T->data=str;
initTree(T->L_Kid);
initTree(T->R_Kid);
}
else T=NULL;
}
void width(Tree *&T,int l)
{
if(T!=NULL)
{
a[l]++;
width(T->L_Kid,l+1);
width(T->R_Kid,l+1);
}
}
int main()
{
Tree *T;
initTree(T);
int l=1;
width(T,l);
int max=a[0];
for(int i=0;i<100;i++)
{
if(a[i]>max) max=a[i];
}
cout<<max;
return 0;
}
相关文章推荐
- 【数据结构_树_Tree_0981】统计利用二叉树存储的森林中树的棵数
- 【数据结构_树_Tree_0971】统计利用先序遍历创建的二叉树的深度
- 【数据结构_树_Tree_0975】利用二叉树统计结点度为2的数
- 【数据结构_树_Tree_0973】利用二叉树计算叶结点个数
- 【数据结构_树_Tree_0983】利用二叉树中序和后序遍历确定二叉树的先序遍历
- 【数据结构_树_Tree_0976】利用二叉树统计度为1的结点
- 【数据结构_树_Tree_0982】利用二叉树储存普通树的度
- 统计利用先序遍历创建的二叉树的宽度
- 【数据结构_树_Tree_0977】计算二叉树中空链域的个数
- 基本数据结构:二叉树(binary tree)
- 基本数据结构:二叉树(binary tree)
- 二叉树系列 - 二叉搜索树 - [LeetCode] 中序遍历中利用 pre节点避免额外空间。题:Recover Binary Search Tree,Validate Binary Search Tree
- python数据结构之二叉树的统计与转换实例
- 基本数据结构——二叉树的建立,遍历,求叶子节点,深度计算
- 数据结构-----后序遍历二叉树非递归算法(利用堆栈实现)
- 利用二叉树的中序遍历和后序遍历序列构造一个二叉树Search results for Construct Binary Tree from Inorder and Postorder Traversa
- SQL 关于如何利用聚合函数把nvarchar型转成int型进行统计计算的方法
- cuda直方图计算——利用shared memory统计直方图
- UVA 548 Tree(数据结构,二叉树,遍历)
- ※数据结构※→☆非线性结构(tree)☆============二叉树 顺序存储结构(tree binary sequence)(十九)