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

LeetCode Hot100:递归穿透值传递问题

题目:104.二叉树的最大深度

1.自底向上依靠return

2.自顶向下依靠全局变量接收

//1.自底向上
/*** Definition for a binary tree node.* function TreeNode(val, left, right) {*     this.val = (val===undefined ? 0 : val)*     this.left = (left===undefined ? null : left)*     this.right = (right===undefined ? null : right)* }*/
/*** @param {TreeNode} root* @return {number}*/
var maxDepth = function (root) {function dfs(root) {if (!root) {return 0;}leftDepth = dfs(root.left);rightDepth = dfs(root.right);return Math.max(leftDepth, rightDepth) + 1;}return dfs(root);
};//自顶向下
/*** Definition for a binary tree node.* function TreeNode(val, left, right) {*     this.val = (val===undefined ? 0 : val)*     this.left = (left===undefined ? null : left)*     this.right = (right===undefined ? null : right)* }*/
/*** @param {TreeNode} root* @return {number}*/
var maxDepth = function (root) {let depth = 0;let ans = 0;function dfs(root, depth) {if (!root) {return;}depth++;ans = Math.max(ans, depth);dfs(root.left, depth);dfs(root.right, depth);}dfs(root, depth);return ans;
};

http://www.xdnf.cn/news/17839.html

相关文章:

  • 艾伦·图灵:计算理论与人工智能的奠基人
  • Java研学-SpringCloud(四)
  • Numerical Difference between vLLM logprobs and huggingface logprobs
  • 数据结构:N叉树 (N-ary Tree)
  • Web 开发 15
  • 4.2 寻址方式 (答案见原书 P341)
  • CIAIE 2025上海汽车内外饰展观察:从美学到功能的产业跃迁
  • Tokenizer(切词器)的不同实现算法
  • 《软件工程导论》实验报告四 详细设计工具
  • 打靶日常-sql注入(手工+sqlmap)
  • 嵌入式学习 day52 IMX6ULL裸机开发-I2C
  • 功能组和功能组状态的概念关系和区别
  • Cursor/VSCode/VS2017 搭建Cocos2d-x环境,并进行正常的调试和运行(简单明了)
  • Docker的相关知识探究详解
  • Linux驱动学习day28(USB驱动,libusb操作)
  • RabbitMQ核心架构与应用
  • DeepSeek-V2:一种强大、经济且高效的混合专家语言模型
  • 区块链技术原理(13)-以太坊燃料费Gas
  • 【数据结构初阶】--排序(三):冒泡排序、快速排序
  • 旋钮键盘项目---foc讲解(开环)
  • 基于WSL搭建Ubuntu 22.04.x LTS开发环境
  • 102、【OS】【Nuttx】【周边】文档构建渲染:安装 Esbonio 服务器
  • Codeforces 无路可走
  • Git代码版本管理
  • 一文打通 AI 知识脉络:大语言模型等关键内容详解
  • Python基础-数据结构
  • 【部署K8S集群】 1、安装前环境准备配置
  • 重塑工业设备制造格局:明远智睿 T113-i 的破局之道
  • 基于多模型的零售销售预测实战指南
  • Spring IOC容器在Web环境中的启动奥秘:深入源码解析