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

临沂网站建设制作网站和网页的区别在于

临沂网站建设制作,网站和网页的区别在于,电子商务网站建设课程设计代码,企业网站seo推广方案提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言一、力扣865. 具有所有最深节点的最小子树二、力扣1123. 最深叶节点的最近公共祖先三、力扣1026. 节点与其祖先之间的最大差值四、力扣1120. 子树的最大平均值 …

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 一、力扣865. 具有所有最深节点的最小子树
  • 二、力扣1123. 最深叶节点的最近公共祖先
  • 三、力扣1026. 节点与其祖先之间的最大差值
  • 四、力扣1120. 子树的最大平均值


前言


二叉树的递归分为「遍历」和「分解问题」两种思维模式,这道题需要用到「分解问题」的思维,而且涉及处理子树,需要用后序遍历

一、力扣865. 具有所有最深节点的最小子树

/*** 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 {public TreeNode subtreeWithAllDeepest(TreeNode root) {Result res = fun(root);return res.node;}public Result fun(TreeNode root){if(root == null){return new Result(null,0);}Result left = fun(root.left);Result right = fun(root.right);if(left.depth == right.depth){return new Result(root,left.depth+1);}Result res = left.depth > right.depth ? left : right;res.depth = res.depth + 1;return res;}
}
class Result{public TreeNode node;public int depth;public Result(TreeNode node, int depth){this.node = node;this.depth = depth;}
}

二、力扣1123. 最深叶节点的最近公共祖先

/*** 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 {public TreeNode lcaDeepestLeaves(TreeNode root) {Result res = fun(root);return res.node;}public Result fun(TreeNode root){if(root == null){return new Result(null,0);}Result left = fun(root.left);Result right = fun(root.right);if(left.depth == right.depth){return new Result(root,left.depth+1);}Result res = left.depth > right.depth ? left : right;res.depth = res.depth + 1;return res;}
}
class Result{public TreeNode node;public int depth;public Result(TreeNode node, int depth){this.node = node;this.depth = depth;}
}

三、力扣1026. 节点与其祖先之间的最大差值

/*** 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 {int res = 0;public int maxAncestorDiff(TreeNode root) {fun(root);return res;}public int[] fun(TreeNode root){if(root == null){return new int[]{Integer.MAX_VALUE,Integer.MIN_VALUE};}int[] leftMinMax = fun(root.left);int[] rightMinMax = fun(root.right);int curMin = Math.min(Math.min(leftMinMax[0],rightMinMax[0]),root.val);int curMax = Math.max(Math.max(leftMinMax[1],rightMinMax[1]),root.val);res = Math.max(res,Math.max(curMax - root.val, root.val - curMin));return new int[]{curMin,curMax};}
}

四、力扣1120. 子树的最大平均值

/*** 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 {double res = 0;public double maximumAverageSubtree(TreeNode root) {fun(root);return res;}public double[] fun(TreeNode root){if(root == null){return new double[]{0,0};}double[] left = fun(root.left);double[] right = fun(root.right);double curCount = left[0] + right[0] + 1;double curSum = left[1] + right[1] + root.val;res = Math.max(res,curSum/curCount);if(curCount == 1){return new double[]{curCount,root.val};}return new double[]{curCount,curSum};}
}
http://www.yayakq.cn/news/677615/

相关文章:

  • 东莞网站哪家好精简wordpress头部信息
  • 阳泉网站建设哪家便宜网站建设规划申请
  • 大型网站建设报价方案主页格式
  • 美食网站开发的特点与总结来安县城乡规划建设局网站
  • 汉中建设网站泰安网站制作方案
  • 换ip对网站有影响吗手机版网站开发人员选项
  • 聊城网站推广动态做防腐木花架的网站
  • 网站界面用什么做广州企业如何建网站
  • 网站建设找超速云建站ui网页设计图片
  • 外贸英语网站下载好字体怎么导入wordpress
  • joomla适合做什么网站苏州保洁公司招聘
  • 中国住房和城乡建设部网站官网怎么做兼职网站
  • 用易语言做攻击网站软件下载书籍教你如何做网站
  • 那个视频网站可以做桌面背景ui设计师找工作
  • 石大远程网页设计与网站建设答案成都品牌设计策划
  • 网络工程师培训机构排名淘宝seo是指什么
  • 页面升级紧急通知班级优化大师官网下载
  • 南京百度网站快速优化申请域名建立网站
  • 一个网站开发的流程代写稿子的平台
  • 建筑公司网站管理员网站怎么做区域性优化
  • 中山网站排名网站速度的重要性
  • 网站免费加速器怎么用wordpress建手机网站
  • 快消品网站建设什么网站做美食最好最专业
  • 什么是网站反向链接有用建站宝盒做网站的吗
  • 福州网站排名优化公司网站的建设哪家好
  • 专门做投票的网站有哪些东莞市朝阳信息网络有限公司
  • 网站推广应该注意什么做办公用品网站资料怎么收集
  • 专业网站定制流程东莞专业做网站建设服务
  • 泰安建材网站建设电话景点介绍网站模板
  • 怎么做免费网站被收录wordpress 设置权限设置