当前位置: 首页 > news >正文

LeetCode hot 100—二叉树的最大深度

题目

给定一个二叉树 root ,返回其最大深度。

二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。

示例

示例 1:

输入:root = [3,9,20,null,null,15,7]
输出:3

示例 2:

输入:root = [1,null,2]
输出:2

分析

深度优先搜索(递归)

核心思想:对于一个二叉树,它的最大深度等于其左子树和右子树的最大深度中的较大值加 1(加上当前根节点)。如果根节点为空,那么深度为 0。

时间复杂度:O(n), n 为二叉树节点的个数

空间复杂度:O(h), h 表示二叉树的高度

class Solution {
public:int maxDepth(TreeNode* root) {if (root == nullptr) {return 0;}int leftDepth = maxDepth(root->left);int rightDepth = maxDepth(root->right);return max(leftDepth, rightDepth) + 1;}
};

广度优先搜索(迭代)

核心思想:通过队列来存储每一层的节点,每遍历完一层,深度加 1。

时间复杂度:O(n), n 为二叉树节点的个数

空间复杂度:O(m),m 是二叉树中节点数最多的那一层的节点数

class Solution {
public:int maxDepth(TreeNode* root) {if (root == nullptr) {return 0;}queue<TreeNode*> nodeQueue;nodeQueue.push(root);int depth = 0;while (!nodeQueue.empty()) {int levelSize = nodeQueue.size();for (int i = 0; i < levelSize; ++i) {TreeNode* current = nodeQueue.front();nodeQueue.pop();if (current->left) {nodeQueue.push(current->left);}if (current->right) {nodeQueue.push(current->right);}}++depth;}return depth;}
};

知识充电

queue 队列

queue(队列)是一种重要的数据结构,遵循先进先出(FIFO, First-In-First-Out)的原则。

基本操作

初始化
#include <queue>
// 定义一个存储 int 类型元素的队列
std::queue<int> myQueue;
入队(push)

push 方法用于将一个元素添加到队列的尾部。

#include <iostream>
#include <queue>
int main() {std::queue<int> myQueue;// 入队操作myQueue.push(10);myQueue.push(20);myQueue.push(30);return 0;
}
出队(pop)

pop 方法用于移除队列头部的元素,但不返回该元素的值。

#include <iostream>
#include <queue>
int main() {std::queue<int> myQueue;myQueue.push(10);myQueue.push(20);myQueue.push(30);// 出队操作myQueue.pop();// 此时队列中剩下 20 和 30return 0;
}
访问头元素(front)

front 方法用于返回队列头部的元素,但不将其从队列中移除。

#include <iostream>
#include <queue>
int main() {std::queue<int> myQueue;myQueue.push(10);myQueue.push(20);myQueue.push(30);// 访问队列头部元素int frontElement = myQueue.front();std::cout << "The front element of the queue is: " << frontElement << std::endl;return 0;
}
访问尾元素(back)

back 方法用于返回队列尾部的元素,但不将其从队列中移除。

#include <iostream>
#include <queue>
int main() {std::queue<int> myQueue;myQueue.push(10);myQueue.push(20);myQueue.push(30);// 访问队列尾部元素int backElement = myQueue.back();std::cout << "The back element of the queue is: " << backElement << std::endl;return 0;
}
http://www.lryc.cn/news/547814.html

相关文章:

  • .h264/.h265文件 前端直接播放
  • 【单片机通信技术】串口通信的几种方式与比较,详细解释SPI通信
  • PDF转JPG(并去除多余的白边)
  • 题目 3217 ⭐成绩统计⭐【滑动窗口 + 二分搜索】蓝桥杯2024年第十五届省赛
  • URL中的特殊字符与web安全
  • 八卡5090服务器首发亮相!
  • esp32驱动带字库芯片TFT屏幕
  • 为AI聊天工具添加一个知识系统 之138 设计重审 之2 文章学 引言之2 附加符号学附属诠释学附随工程学(联系)
  • java环境部署
  • 正点原子[第三期]Arm(iMX6U)Linux移植学习笔记-2.1 uboot简介
  • CentOS 7.9 安装 ClickHouse 文档
  • 高考數學。。。
  • 使用GitLink个人建站服务部署Allure在线测试报告
  • Linux 上离线安装 python3
  • js操作字符串的常用方法
  • 自动化学习-使用git进行版本管理
  • GCC RISCV 后端 -- GCC Passes 注释
  • Ollama存在安全风险的情况通报及解决方案
  • IDEA Generate POJOs.groovy 踩坑小计 | 生成实体 |groovy报错
  • 阿里云云监控资源告警常用模板
  • Tailwind CSS 问题:npm error could not determine executable to run
  • vue基本功
  • .NET10 - 预览版1新功能体验(一)
  • java下载多个网络文件并压缩成压缩包保存到本地
  • 23种设计模式之单例模式(Singleton Pattern)【设计模式】
  • [项目]基于FreeRTOS的STM32四轴飞行器: 四.LED控制
  • 使用 dynamic-datasource-spring-boot-starter 实现多数据源动态切换
  • springboot中注解有什么用
  • Spring Boot 缓存最佳实践:从基础到生产的完整指南
  • Linux网络相关内容与端口