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

ksweb搭建网站教程专业的seo外包公司

ksweb搭建网站教程,专业的seo外包公司,上海电商网站建设公司,注销备案号 网站文章目录 题目描述思路复杂度Code 题目描述 思路 遍历思想(利用二叉树的中序遍历) 本题的难点在于可能存在多个答案,并且要返回最小的那一个,为了解决这个问题,我门则要利用上二叉搜索树中序遍历为有序序列的特性,具体到代码中&a…

文章目录

  • 题目描述
  • 思路
  • 复杂度
  • Code

题目描述

在这里插入图片描述在这里插入图片描述

思路

遍历思想(利用二叉树的中序遍历)

本题的难点在于可能存在多个答案,并且要返回最小的那一个,为了解决这个问题,我门则要利用上二叉搜索树中序遍历为有序序列的特性,具体到代码中(结合代码看):
1.我们用变量res记录最终的结果,同时在中序遍历位置处利用Math.abs(root.val - target) < Math.abs(res - target)边遍历边更新res的值(注意此处是小于号
2.根据 target 和 root.val 的相对大小决定去左右子树搜索:如果 target 比 root 大,那么 root 的左子树差值肯定更大,直接遍历右子树;如果 target 比 root 小,那么 root 的右子树差值肯定更大,直接遍历左子树
3.同时要注意深刻体会
二叉树的中序遍历
(即是在二叉树中遍历完当前根节点的左子树后再准备遍历右子树的时刻)

复杂度

时间复杂度:

O ( n ) O(n) O(n);其中 n n n为二叉树的节点个数

空间复杂度:

O ( h ) O(h) O(h);其中 h h h为二叉树的高度

Code

class Solution {int res = Integer.MAX_VALUE;public int closestValue(TreeNode root, double target) {traverse(root, target);return res;}// Write the if judgment logic in the middle order// so that it can be executed from small to large,// ensuring that the final result is the smallest valueprivate void traverse(TreeNode root, double target) {if (root == null) {return;}// Depending on the relative size of target and root.val,// search the left and right subtreesif (root.val < target) {// Mid-order position if (Math.abs(root.val - target) < Math.abs(res - target)) {res = root.val;}// If target is larger than root,// then root's left subtree difference must be larger,// and the right subtree is traversed directlytraverse(root.right, target);} else {// If target is smaller than root,// then root's right subtree difference must be larger,// and the left subtree is traversed directlytraverse(root.left, target);// Mid-order position if (Math.abs(root.val - target) < Math.abs(res - target)) {res = root.val;}}}
}
http://www.hkea.cn/news/536083/

相关文章:

  • 企业品牌网站营销关于seo的行业岗位有哪些
  • 群晖怎样做网站惠州网络营销
  • 怎么做网站内部链接的优化品牌营销推广代运营
  • 婚纱照网站制作搜索引擎优化搜索优化
  • 网站建设吉金手指专业13中国营销网站
  • 做销售在哪些网站注册好百度产品
  • dw做汽车网站seo排名工具提升流量
  • 网络培训学校排名奉化seo页面优化外包
  • vps除了做网站还能做什么晨阳seo服务
  • seo网站建设优化什么意思网络营销与直播电商专业就业前景
  • 工程建设企业网站网站关键词优化应该怎么做
  • 修复wordpress青岛网站优化
  • 敦煌网站做外贸怎样网页推广怎么做的
  • 南京网站建设优化今日头条普通版
  • 网站编辑的工作职能有哪些活动营销案例100例
  • 小程序招商加盟平台我是seo关键词
  • wordpress 发帖机镇江抖音seo
  • 网站建设的小结可以发外链的论坛有哪些
  • 网站正常打开速度网店营销与推广策划方案
  • 义乌 网站制作进入百度app
  • 做外围网站赌球红树林seo基础入门免费教程
  • 绿色风格网站seo排名赚钱
  • 南宁企业免费建站百度推广营销怎么做
  • 建立个人网站的成本短视频seo营销系统
  • 深圳公司名称大全网站结构优化的内容和方法
  • 安康市代驾公司上海网站关键词排名优化报价
  • 怎么在网站上建设投票统计在线培训系统app
  • 泰州网站建设哪家好网站seo的主要优化内容
  • 洛卡博网站谁做的seo权重查询
  • 东莞网络科技公司有哪些山东网站seo