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

news/2025/2/1 12:57:30/

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/news/1568430.html

相关文章

Mac m1,m2,m3芯片使用nvm安装node14报错

使用nvm安装了node 12/16/18都没有问题,到14就报错了。第一次看到这个报错有点懵,查询资料发现是Mac芯片的问题。 Issue上提供了两个方案: 1、为了在arm64的Mac上安装node 14,需要使用Rosseta,可以通过以下命令安装 …

边缘计算与ROS结合:如何实现分布式机器人智能决策?

前言 在现代机器人系统中,分布式决策能力正成为实现群体协作任务的关键需求。传统集中式架构存在决策延迟、通信瓶颈以及容错性低等问题,而边缘计算结合 ROS(Robot Operating System)为分布式机器人智能决策提供了全新的解决方案…

react native i18n插值:跨组件trans

想要实现动态插值以及插入元素,如下效果 这个找了蛮久的,官网的例子在我这无效,所以网上找了比较久,没能理解用法。最后是在 github issue 中看到别人的用法,自己理解下实现出来了,所以这里记录下。 例如…

OpenEuler学习笔记(十六):搭建postgresql高可用数据库环境

以下是在OpenEuler系统上搭建PostgreSQL高可用数据环境的一般步骤,通常可以使用流复制(Streaming Replication)或基于Patroni等工具来实现高可用,以下以流复制为例: 安装PostgreSQL 配置软件源:可以使用O…

libOnvif通过组播不能发现相机

使用libOnvif库OnvifDiscoveryClient类, auto discovery new OnvifDiscoveryClient(QUrl(“soap.udp://239.255.255.250:3702”), cb.Build()); 会有错误: end of file or no input: message transfer interrupted or timed out(30 sec max recv delay)…

【Block总结】HiLo注意力,局部自注意力捕获细粒度的高频信息,通过全局注意力捕获低频信息|即插即用

一、论文信息 标题: Fast Vision Transformers with HiLo AttentionGitHub链接: https://github.com/ziplab/LITv2论文链接: arXiv 二、创新点 HiLo注意力机制: 本文提出了一种新的自注意力机制——HiLo注意力,旨在同时捕捉图像中的高频和低频特征。该机制通过将…

【NEXT】网络编程——上传文件(不限于jpg/png/pdf/txt/doc等),或请求参数值是file类型时,调用在线服务接口

最近在使用华为AI平台ModelArts训练自己的图像识别模型,并部署了在线服务接口。供给客户端(如:鸿蒙APP/元服务)调用。 import核心能力: import { http } from kit.NetworkKit; import { fileIo } from kit.CoreFileK…

使用kitty terminal遇到的‘xterm-kitty‘: unknown terminal type.

解决办法 方式一 export TERMxterm-256color使永久生效 echo export TERMxterm-256color >> ~/.zshrc # 如果用 zsh,如果使用的是bash就修改为bashrc source ~/.zshrc #同理如果是ssh下遇到该问题,参考 https://sw.kovidgoyal.net/kitty/faq/…