码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 6-8 最宽层次结点数 分数 10


    文章目录

    • 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)
    		return 0;
    	if (i == 1) 
    		return 1;
    	return LevelNodeCount(T->lchild, i - 1) + LevelNodeCount(T->rchild, i - 1);
    }
    int GetDepthOfBiTree(BiTree T)
    {
    	if (T == NULL)
    		return 0;
    	return GetDepthOfBiTree(T->lchild) > GetDepthOfBiTree(T->rchild) ? 
    		   GetDepthOfBiTree(T->lchild) + 1
    		 : GetDepthOfBiTree(T->rchild) + 1;
    }
    int MaxWidth(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;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    2.2法二: 顺序队列实现层序遍历

    int MaxWidth(BiTree T) 
    {
    	if (T == NULL)
    		return 0;
    	
    	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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    3.C++层序遍历求最大宽度

    3.1层序遍历代码

    void LevelTraverse(BiTNode* T)
    {
    	if (T == nullptr)
    		return;
    
    	queue<struct BiTNode*> q;
    	q.push(T);
    
    	while (!q.empty())
    	{
    		BiTNode* front = q.front();
    		cout << front->data;
    		q.pop();
    
    		if (front->lchild)
    			q.push(front->lchild);
    		if (front->rchild)
    			q.push(front->rchild);
    	}
    	cout << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    3.2求最大宽度

    typedef char ElemType;
    typedef struct BiTNode
    {
    	ElemType data;
    	struct BiTNode* lchild, * rchild;
    }BiTNode, * BiTree;
    int MaxWidth(BiTree T)
    {
    	if (T == nullptr)
    		return 0;
    
    	queue<BiTree> q;
    	q.push(T);
    	int max = 0;
    
    	while (!q.empty())
    	{
    		//当前层结点数
    		int perLevel = q.size();  
    		if (perLevel > max)
    			max = perLevel;
    
    		//for循环的作用:
    		//遍历当前栈中的结点 拿出一个结点node 把它的孩子入栈后就删除node
    		//此时栈中存的结点是下一层结点
    		for (int i = 0; i < perLevel; i++)
    		{
    			BiTree front = q.front();
    			q.pop();
    
    			if (front->lchild) 
    				q.push(front->lchild);
    			if (front->rchild)
    				q.push(front->rchild);
    		}
    	}
    	return max;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
  • 相关阅读:
    面试--并发多线程基础
    编写一个人民币与输入币种的兑换程序,根据用户输入的币种、汇率和待兑换人民币数量,实现人民币到其他币种的兑换。
    lv5 嵌入式开发-7 有名管道和无名管道
    22-06-25 西安 linux(02) 命令、shell脚本开发、linux安装软件
    npm install失败的分析与解决方案,以及修复完成的代码地址
    使用C#创建服务端Web API
    OpenCV-17制作LOGO小练习
    Python将字符串转换成dataframe
    1108 String复读机分数 20
    关于Thread 类及其基本用法
  • 原文地址:https://blog.csdn.net/LHRan_ran_/article/details/134203960
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号