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

算法通关村第八关|白银|二叉树的深度和高度问题【持续更新】

1.最大深度问题(后序遍历)

只需要一直递归,维护一个最大值。每一层只要有一个子节点,这个最大值就可以增加。

public int maxDepth(TreeNode root) {if (root == null) {return 0;}int leftHeight = maxDepth(root.left);int rightHeight = maxDepth(root.right);// 这里要+1,不要漏掉root节点return Math.max(leftHeight, rightHeight) + 1;
}

2.最大深度问题(层次遍历)

遍历出多少层,那么最大深度就是多少。

public int maxDepth(TreeNode root) {if (root == null) {return 0;}Queue<TreeNode> queue = new LinkedList<TreeNode>();queue.offer(root);int ans = 0;while (!queue.isEmpty()) {int size = queue.size();while (size > 0) {TreeNode node = queue.poll();if (node.left != null) {queue.offer(node.left);}if (node.right != null) {queue.offer(node.right);}size--;}ans++;}return ans;
}

3.判断高度平衡二叉树

本题中,一棵高度平衡二叉树定义为:一个二叉树每个节点的左右两个子树的高度差的绝对值不超过 1 。
二叉树的高度:从该节点到叶子节点的节点数。

public Solution {public boolean isBalanced(TreeNode root) {return height(root) >= 0;}public int height(TreeNode root) {if (root == null) {return 0;}int leftHeight = height(root.left);int rightHeight = height(root.right);if (leftHeight == -1 || rightHeight == -1 || Math.abs(leftHeight - rightHeight) > 1) {return -1;} else {return Math.max(leftHeight, rightHeight) + 1;}}
}

4.最小深度(递归)

最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

是要有叶子节点才可以算最小深度的,所以当一个节点的一个子树为空,一个不为空的时候,要从不为空的子树继续算深度,不能直接返回结果。

public int minDepth(TreeNode root) {if (root == null) {return 0;}if (root.left == null && root.right == null) {return 1;}// 定义一个min_depth,比较left和right的大小(如果不为空的话)int min_depth = Integer.MAX_VALUE;if (root.left != null) {min_depth = Math.min(minDepth(root.left), min_depth);}if (root.right != null) {min_depth = Math.min(minDepth(root.right), min_depth);}return min_depth + 1;
}

5.最小深度(层次遍历)

找到第一个叶子节点即可。

public int minDepth(TreeNode root) {if (root == null) {return 0;}int minDepth = 0;LinkedList<TreeNode> queue = new LinkedList<TreeNode>();queue.add(root);while (queue.size() > 0) {int size = queue.size();minDepth++;for (int i = 0; i < size; i++) {TreeNode node = queue.poll();// 先判断是不是叶子节点,是就直接返回值if (node.left == null && node.right == null) {return minDepth;}if (node.left != null) {queue.add(node.left);}if (node.right != null) {queue.add(node.right);}}}return 0;
}

6.N叉树的最大深度

N叉树的定义:

class Node {public int val;public List<Node> children;public Node() {}public Node(int _val) {val = _val;}public Node(int _val, List<Node> _children) {val = _val;children = _children;}
}

N叉树的最大深度(递归)

用一个增强 for 循环遍历每个子树即可。

class Solution {public int maxDepth(Node root) {if (root == null) {return 0;} else if (root.children.isEmpty()) {return 1;} else {int max = 0;for (Node item : root.children) {int childrendepth = maxDepth(item);max = Math.max(max, childrendepth);}return max + 1;}}
}

N叉树的最大深度(层次遍历)

【持续更新】。

如果对您有帮助,请点赞关注支持我,谢谢!❤
如有错误或者不足之处,敬请指正!❤
个人主页:星不易 ❤
算法通关村专栏:不易|算法通关村 ❤

http://www.lryc.cn/news/222864.html

相关文章:

  • cmake 之add_definitions使用误区
  • Leetcode—515.在每个树行中找最大值【中等】
  • 安防监控系统EasyCVR平台设备通道绑定AI算法的功能设计与开发实现
  • element 弹窗浏览器后退-遮照层还存在问题 以及跟vue keep-alive冲突
  • C++(Qt)软件调试---自动注册AeDebug(17)
  • 云原生周刊:Gateway API 1.0.0 发布 | 2023.11.6
  • Java2 - 数据结构
  • 精解括号匹配问题与极致栈设计:揭开最大栈和最小栈的奥秘
  • 云存储/视频监控管理平台EasyCVR,使用sqlite数据库出现卡顿该如何优化?
  • 实战!工作中常用的设计模式
  • MySQL进阶_1.逻辑架构和SQL执行流程
  • 基于GCC的工具objdump实现反汇编
  • 排序算法的空间复杂度和时间复杂度
  • 【电路笔记】-基尔霍夫电路定律
  • 从零开始搭建React+TypeScript+webpack开发环境-基于axios的Ajax请求工具
  • 【uniapp小程序下载】调用uni.uploadfile方法在调试工具里是没有问题的,但是线上版本和体验版就调用不成功,真机调试也没问题
  • chatGLM中GLM设计思路
  • 卡牌游戏类型定制开发微信卡牌小程序游戏
  • web —— css(1)
  • 站群服务器的特性和好处是什么
  • 竞赛 行人重识别(person reid) - 机器视觉 深度学习 opencv python
  • 软件设计模式的意义
  • vue基础知识十八:说说你对keep-alive的理解是什么?
  • Linux CentOS配置阿里云yum源
  • ESP32网络开发实例-Web服务器以仪表形式显示传感器计数
  • @Bean有哪些属性
  • 【Qt之绘制兔纸】
  • JS+CSS随机点名详细介绍复制可用(可自己添加人名)
  • 西瓜书笔记
  • 学算法常用刷题网站