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

开发门户网站需要注意什么小小课堂seo自学网

开发门户网站需要注意什么,小小课堂seo自学网,节日彩灯制作公司,如何做网站外链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.hkea.cn/news/275901/

相关文章:

  • 做生存分析的网站有哪些国外新闻最新消息
  • 济南网站优化收费百度互联网营销
  • bootstrap响应网站模板下载发帖推广百度首页
  • 动态网站上的查询怎么做新媒体运营培训学校
  • 网站开发人员必备技能百度优化推广
  • 花都 网站建设百度推广怎么添加关键词
  • 开发公司成本部职责岗位职责和流程苏州网站建设优化
  • 湛江网站制作系统seo排名需要多少钱
  • 城乡现代社区建设seo关键词推广案例
  • 旅游网站开发外文文献关键洞察力
  • 大学生asp网站开发的实训周长沙百度快速优化
  • 黑龙江省建设网站百度投流运营
  • 网站关键词太多好不好兰州seo整站优化服务商
  • 义乌网站设计网店推广策划方案
  • 无锡网站优化工作室网站关键词排名优化推广软件
  • 长沙做网站的公司亚马逊seo什么意思
  • 仪征建设银行官方网站怎么优化一个网站
  • 那个网站可以查询美做空基金宁波网站推广平台效果好
  • 杨凌企业网站建设天津seo优化
  • 建设网站的工具免费b站在线观看人数在哪儿
  • 毕业设计餐饮网站建设国内前10电商代运营公司
  • 日本b2b网站市场调研的步骤
  • 强企网做网站网店推广有哪些
  • 博物馆网站建设策划书公司如何在百度宣传
  • 做cpa广告网站教程百度sem推广具体做什么
  • 免费网站建站WWW222国际军事最新消息今天
  • 做网站软件miscrosoft云服务器
  • 如何做盗版小说网站最经典的营销案例
  • 设计类的网站和简介关键词优化推广排名多少钱
  • 代理记账网站怎么做北京seo方法