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

网站制作背景哔哩哔哩h5播放器

网站制作背景,哔哩哔哩h5播放器,wordpress安装博客,创新的成都 网站建设1.最大深度问题(后序遍历) 只需要一直递归,维护一个最大值。每一层只要有一个子节点,这个最大值就可以增加。 public int maxDepth(TreeNode root) {if (root null) {return 0;}int leftHeight maxDepth(root.left);int right…

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.yayakq.cn/news/397307/

相关文章:

  • 类似站酷的网站建站wordpress基础安装
  • 做购物网站是怎么连接银行电子商务网站开发教程课后习题
  • 中小企业网站营销fedora做网站服务器
  • 橙云 php网站建设wordpress 拖拽页面
  • 设计素材网站官网2024新闻热点摘抄
  • 网站汇总表怎么做建设自己的网站步骤
  • 物流企业网站源码怎么在网站后台加框框
  • 徐州网站建设技术外包wordpress 知识 管理
  • 1_ 掌握网站开发的基本流程 要求:熟悉网站开发与设计的基本流程.北京华夏工程建设监理公司网站
  • 绝味鸭脖网站建设规划书陕西住房建设厅考试官方网站
  • 网站做宣传大学生兼职网站开发毕设论文
  • 简述网站建设的流程免费云服务器主机
  • 网页设计与网站建设在线第二章移动网站开发与维护
  • 做电脑网站用什么软件网站域名注册
  • 玩具网站建设策划书网站推广规范
  • 便宜的做网站公司专业的vi设计公司
  • 服务好的网站建设平台平顶山专业做网站公司
  • wordpress做动漫网站铜仁搜狗推广
  • 机关网站建设总结如何禁止通过ip访问网站
  • 天津网站优化收费app推广方法及技巧
  • 微网站开发需要几个人网站建设动画教程
  • 网站建设的技术风险分析与规避app开发公司有哪些流程
  • wdcp网站打不开河南省汝州市文明建设门户网站
  • wordpress搭建ss网站描述怎么写利于seo
  • 网站开发需要学php吗wordpress商业源码
  • 协会网站改版建议宁夏省建筑信息平台
  • 设备建设网站seo顾问是啥
  • 保护稀有动物网站建设策划书镭拓网站建设官网
  • 网站开发安全性枣强网站建设电话
  • 昆山高端网站建设公司哪家好做一个简单的网页游戏