Day16——二叉树的最大深度、二叉树的最小深度、完全二叉树的节点个数

简介: Day16——二叉树的最大深度、二叉树的最小深度、完全二叉树的节点个数

前言


今日文案:

人生总会有不期而遇的温暖和生生不息的希望,前提是你必须足够去努力,才不会错过美丽的风景和动人的故事,早安~

一、二叉树最大深度


题目来源:

力扣

解题思路1(层序):

我们只要广度搜索,一层一层遍历,然后一层一层计数,然后最后我们就可以找出最大深度。

class Solution {
public:
    int maxDepth(TreeNode* root) {
        int deep=0;
        queue<TreeNode*> que;        //队列
        if(root)
        que.push(root);
        while(!que.empty())        
        {
            int size=que.size();        //每层
            for(int i=0;i<size;i++)
            {
                TreeNode*p=que.front();
                que.pop();
                if(p->left)
                que.push(p->left);
                if(p->right)
                que.push(p->right);
            }
            deep++;                    //每层加1
        }
        return deep;
    }
};

解题思路2(递归):


解题思路:

左边遍历,右边遍历,记录返回值。

class Solution {
public:
    int getdepth(TreeNode*root)
    {
        if(root==0)
        return 0;
        int leftdepth=getdepth(root->left);    //记录左边深度
        int rightdepth=getdepth(root->right);  //记录右边深度
        int res=max(leftdepth,rightdepth);        //返回最大深度
        return res+1;                        //加1 返回上一层
    } 
    int maxDepth(TreeNode* root) {
        return getdepth(root);
    }
};

二、二叉树的最小深度


题目链接:


力扣

class Solution {
public:
    int minDepth(TreeNode *root) {
        if (root == nullptr) {
            return 0;
        }
        if (root->left == nullptr && root->right == nullptr) {        //判断左右
            return 1;                                //叶子节点深度+1返回上一层
        }
        int min_depth = INT_MAX;
        if (root->left != nullptr) {        
            min_depth = min(minDepth(root->left), min_depth);
        }
        if (root->right != nullptr) {
            min_depth = min(minDepth(root->right), min_depth);
        }
        return min_depth + 1;
    }
}

三、n叉树的最大深度


题目来源:

力扣

具体思路上一篇文章有哇。

class Solution {
public:
    int maxDepth(Node* root) {
        if(root==0)
        return 0;
        int res=0;
        for(auto &x:root->children)        //把节点遍历进去
        {
            int D=maxDepth(x);
            res=max(D,res);
        }
            return res+1;
    }
};

四、完全二叉树的节点个数


题目来源:

力扣

1、解题思路(层序)


一层一层往下刷,记录数值就可以,因为已经给定了一颗完全二叉树

class Solution {
public:
    int countNodes(TreeNode* root) {
        queue<TreeNode*> que;
        if (root != NULL) que.push(root);
        int result = 0;
        while (!que.empty()) {
            int size = que.size();
            for (int i = 0; i < size; i++) {
                TreeNode* node = que.front();
                que.pop();
                result++;   // 记录节点数量
                if (node->left) que.push(node->left);
                if (node->right) que.push(node->right);
            }
        }
        return result;
    }
};

2、解题思路(递归)


先求它的左子树的节点数量,再求的右子树的节点数量,最后取总和再加一 (加1是因为算上当前中间节点)就是目前节点为根节点的节点数量。

class Solution {
public:
    int count=0;
    int getcount(TreeNode*root)
    {
        if(root==NULL)
        {
            return 0;
        }
        int leftNode=getcount(root->left);        //清点左右节点数量
        int rightNode=getcount(root->right);
        return count=leftNode+rightNode+1;        //+1把节点数量返回上一层
    }
    int countNodes(TreeNode* root) {
        if(root==0)
        return 0;
        return getcount(root);
    }
};

总结


冲冲冲!!!

相关文章
|
前端开发 JavaScript BI
Django教程第5章 | Web开发实战-数据统计图表(echarts、highchart)
使用echarts和highcharts图表库实现折线图、柱状图、饼图和数据集图
364 2
|
Docker 容器 Ubuntu
docker container 设置编码为utf8
以Ubuntu 14.04 为例创建编码为utf8的container
6118 0
|
4月前
|
Linux 虚拟化 iOS开发
Debian 13 x86_64 OVF (sysin) - VMware 虚拟机模板
Debian 13 x86_64 OVF (sysin) - VMware 虚拟机模板
350 0
Debian 13 x86_64 OVF (sysin) - VMware 虚拟机模板
|
安全 Java API
【性能与安全的双重飞跃】JDK 22外部函数与内存API:JNI的继任者,引领Java新潮流!
【9月更文挑战第7天】JDK 22外部函数与内存API的发布,标志着Java在性能与安全性方面实现了双重飞跃。作为JNI的继任者,这一新特性不仅简化了Java与本地代码的交互过程,还提升了程序的性能和安全性。我们有理由相信,在外部函数与内存API的引领下,Java将开启一个全新的编程时代,为开发者们带来更加高效、更加安全的编程体验。让我们共同期待Java在未来的辉煌成就!
308 11
|
开发框架 安全 .NET
使用grpcui测试gRPC服务
使用grpcui测试gRPC服务
383 0
|
监控 JavaScript API
使用 requestAnimationFrame 提升 web 性能
使用 requestAnimationFrame 提升 web 性能
341 1
|
监控 物联网 测试技术
【好用的个人工具】使用Docker部署Dashdot服务器仪表盘
【5月更文挑战第15天】使用Docker部署Dashdot服务器仪表盘
358 13
|
Shell 网络安全 Docker
【Docker系列】docker镜像与容器基本操作命令(二)
【Docker系列】docker镜像与容器基本操作命令(二)
【Docker系列】docker镜像与容器基本操作命令(二)
|
存储 机器学习/深度学习 关系型数据库
mysql中char和varchar的区别
mysql中char和varchar的区别
476 1
|
前端开发 JavaScript 搜索推荐
SPA与MPA:如何选择?
在互联网技术的发展中,单页应用(SPA)和多页应用(MPA)逐渐成为两种主流开发方式。本文将比较SPA和MPA的优缺点,帮助读者更好地选择适合自己的应用类型。