啥是二叉树的宽度呢?就是每一层节点个数的最大值!这个和用非递归算法求二叉树的高度极其相似,就是多了一个擂台赛而已!
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
typedef struct bnode
{
char data;
struct bnode *lchild,*rchild;
}BNode,*bitree;
bitree pre_create()//创建二叉树
{
bitree bt;
char ch;
ch=getchar();
if(ch==' ')
return NULL;
else
{
bt=new BNode[sizeof(BNode)];
bt->data=ch;
bt->lchild=pre_create();
bt->rchild=pre_create();
}
}
int width(bitree bt)//求二叉树宽度
{
queue<bitree> q;
bitree p=bt;
int len,width;
if(!bt)
return 0;
q.push(p);
width=len=q.size();//初始化,width表示当前比较过程中每一层节点个数的最大值,len为循环