二叉树的最大深度(遍历思想+分解思想)

ops/2025/2/1 10:54:59/

Problem: 104. 二叉树的最大深度

文章目录

  • 题目描述
  • 思路
  • 复杂度
  • Code

题目描述

在这里插入图片描述在这里插入图片描述

思路

遍历思想(实则二叉树的先序遍历)

1.欲望求出最大的深度,先可以记录一个变量res,同时记录每次当前节点所在的层数depth
2.在递的过程中,每次递一层,也即使当前又往下走了一层,则depth++,当到达叶子节点时,比较并取出max【res, depth】
3.在归的过程中,因为是在往上层归,则depth–;
4.返回最终的res即可

分解思想

1.要求整个树的最大深度则可以分解其为求去当前节点的左右子树的最大深度再加当前节点的高度1

复杂度

二者均为
时间复杂度:

O ( n ) O(n) O(n);其中 n n n为二叉树的节点个数

空间复杂度:

O ( h ) O(h) O(h);最坏空间复杂度

Code

遍历思想

java">/**
/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {// recode the maximum depth int res = 0;// recode the depth of the traversed nodeint depth = 0;public int maxDepth(TreeNode root) {traverse(root);return res;}public void traverse(TreeNode root) {if (root == null) {return;}depth++;if (root.left == null && root.right == null) {res = Math.max(res, depth);}traverse(root.left);traverse(root.right);depth--;}
}

分解思想

java"> class Solution {// Definition: Given the root node, return the maximum depth of the binary treepublic int maxDepth(TreeNode root) {if (root == null) {return 0;}// calculate the maximum depth of the left and right subtreesint leftMax = maxDepth(root.left);int rightMax = maxDepth(root.right);// The maximum depth of the entire tree is// the maximum of the left and right subtree// plus one for the root node itselfint res = Math.max(leftMax, rightMax) + 1;return res;}
}

http://www.ppmy.cn/ops/154718.html

相关文章

蓝桥云课下载(jdk11、eclipse、idea)

目录 下载jdk11下载eclipse下载idea 下载jdk11 下载地址: (自用) https://www.lanqiao.cn/courses/44495/learning/?id3144371&compatibilityfalse 安装步骤: 双击 -> -> 下一步 -> 配置环境变量(略…

【深度之眼cs231n第七期】笔记(三十一)

目录 强化学习什么是强化学习?马尔可夫决策过程(MDP)Q-learning策略梯度SOTA深度强化学习 还剩一点小尾巴,还是把它写完吧。(距离我写下前面那行字又过了好几个月了【咸鱼本鱼】)(汗颜&#xff…

在sortablejs的拖拽排序情况下阻止input拖拽事件

如题 问题 在vue3的elementPlus的table中,通过sortablejs添加了行拖拽功能,但是在行内会有输入框,此时拖拽输入框会触发sortablejs的拖拽功能 解决 基于这个现象,我怀疑是由于拖拽事件未绑定而冒泡到后面的行上从而导致的拖拽…

1 HDFS

1 HDFS 1. HDFS概述2. HDFS架构3. HDFS的特性4. HDFS 的命令行使用5. hdfs的高级使用命令6. HDFS 的 block 块和副本机制6.1 抽象为block块的好处6.2 块缓存6.3 hdfs的文件权限验证6.4 hdfs的副本因子 7. HDFS 文件写入过程(非常重要)7.1 网络拓扑概念7.…

IDEA创建修改gitee仓库

一、创建gitee仓库 创建gitee仓库 点击复制仓库地址 右击项目名 --> Open In --> Terminal 初始化仓库 git init 添加仓库地址 复制之前创建的仓库地址 git remote add origin 仓库地址 二、修改IDEA的gitee仓库 查询当前项目所在仓库 git remote -v 删除原仓库…

Vue.js 响应式引用与响应式数据(`ref` 和 `reactive`)

Vue.js 响应式引用与响应式数据(ref 和 reactive) 今天我们来聊聊 Vue 3 中的两个核心概念:ref 和 reactive。如果你对如何在 Vue 3 中创建响应式数据感到困惑,那么这篇文章将为你解答。 ref 和 reactive 的区别 在 Vue 3 中&a…

C# 探秘:PDFiumCore 开启PDF读取魔法之旅

一、引言 在当今数字化时代,PDF 文件就像一个个神秘的宝盒,里面装满了各种信息。无论是项目文档、学术论文还是产品说明书,PDF 格式凭借其良好的兼容性和稳定性,成为了信息传递的重要载体。想象一下,你接到一个紧急任…

deepseek-r1(Mac版 安装教程)

文章目录 deepseek-r1安装教程(Mac)1. 安装ollama2. 本地下载对应的模型3. 使用3.1 终端直接使用3.2 网页使用 deepseek-r1安装教程(Mac) 1. 安装ollama 如果之前没有安装过ollama的,需要在ollama官网下载对应系统的o…