聊城网站优化网络推广,百度竞价排名公式,什么网站做的靠枕比较有创意,万网注册的域名怎么建设网站文章目录 1.题目描述2.本题ac答案2.1法一: 代码复用2.2法二: 顺序队列实现层序遍历 3.C层序遍历求最大宽度3.1层序遍历代码3.2求最大宽度 1.题目描述 2.本题ac答案
2.1法一: 代码复用 //二叉树第i层结点个数
int LevelNodeCount(BiTree T, int i)
{if (T NULL || i < 1)re…
文章目录
1.题目描述
2.本题ac答案
2.1法一: 代码复用
2.2法二: 顺序队列实现层序遍历
3.C++层序遍历求最大宽度
3.1层序遍历代码
3.2求最大宽度
1.题目描述
2.本题ac答案
2.1法一: 代码复用
//二叉树第i层结点个数intLevelNodeCount(BiTree T,int i){if(T ==NULL|| i <1)return0;if(i ==1)return1;returnLevelNodeCount(T->lchild, i -1)+LevelNodeCount(T->rchild, i -1);}intGetDepthOfBiTree(BiTree T){if(T ==NULL)return0;returnGetDepthOfBiTree(T->lchild)>GetDepthOfBiTree(T->rchild)?GetDepthOfBiTree(T->lchild)+1:GetDepthOfBiTree(T->rchild)+1;}intMaxWidth(BiTree T){int per =0;int max =0;for(int i =1; i <=GetDepthOfBiTree(T); i++){per =LevelNodeCount(T, i);if(per > max)max = per;}return max;}
2.2法二: 顺序队列实现层序遍历
intMaxWidth(BiTree T){if(T ==NULL)return0;BiTree queue[100]={0};BiTree cur =NULL;int begin =0, end =0;int perLevel =0, max =0;//每入队一个结点 end++表示有效数据加一queue[end++]= T;//begin != end: 队中还有结点 还未取到上一层所有结点的子结点while(begin != end){perLevel = end - begin;if(perLevel > max)max = perLevel;//cur指向队头结点 (马上就要被遗弃 因为已经被访问)//begin++表示当前结点已被遍历 当前结点被遗弃cur = queue[begin++];if(cur->lchild)queue[end++]= cur->lchild;if(cur->rchild)queue[end++]= cur->rchild;}return max;}