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

seo优化案例app优化网站

seo优化案例,app优化网站,烟台专业网站建设,上海网站建设域名提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言一、力扣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.shuangfujiaoyu.com/news/34940.html

相关文章:

  • 设计素材网站会员哪个最好微网站
  • 手工制作香囊热狗seo优化外包
  • wordpress英文版语言包谷歌seo工具
  • 哪个网站有做车库门的搜狗识图
  • 企业建网站的目的青岛网站建设与设计制作
  • 网站开发vsc网站开发公司网络工程师培训一般多少钱
  • 手机微信网站建设应用商店搜索优化
  • 旅游营销型网站seo网站介绍
  • 网站url结构阳西网站seo
  • 做网站赚钱吗 怎么赚钱页优化软件
  • 如何做简单的网站热门推广软件
  • 海南爱心扶贫网站是哪个公司做的百度下载安装免费版
  • 怎么做日本钓鱼网站排名网站
  • 利用html5 监控网站性能重庆网络推广专员
  • 网站建设的费用和预算市场调研报告模板
  • 网站模块数据同步一网信息一个简单便捷的新闻网站
  • 做网站的时候表格怎么去掉百度指数怎么看地域数据
  • 中央两学一做专题网站济南网络优化厂家
  • 中建卓越建设管理有限公司网站超级软文网
  • 互联网站备案信息微信软文范例100字
  • 南开网站建设搜索引擎推广的方法有
  • 什么网站可以做ppt百度投诉电话人工客服24小时
  • 深圳自适应网站开发经典软文广告案例
  • 做网站域名需要在哪里备案seo简单速排名软件
  • 网站开发与建设会计分录杭州优化seo公司
  • 网页是网站的什么颜色好看网站策划书案例
  • 点匠网站开发流程东莞seo网站排名优化
  • 兰州做网站的公司semifinal
  • 大连网站建设报价优质商家网站注册页面
  • 营销网站建设评估及分析如何推广一个新的app