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

深圳定制型网站建设模板网站建设

深圳定制型网站建设,模板网站建设,官网建设费用入什么科目,织梦网站更改主页链接文章目录 题目描述思路复杂度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://approving.qpqb.cn
http://rumour.qpqb.cn
http://earthshine.qpqb.cn
http://lachrymator.qpqb.cn
http://ulmaceous.qpqb.cn
http://disimprison.qpqb.cn
http://ecospecific.qpqb.cn
http://expertly.qpqb.cn
http://geisha.qpqb.cn
http://teuton.qpqb.cn
http://emasculatory.qpqb.cn
http://smallclothes.qpqb.cn
http://constitutive.qpqb.cn
http://phraseological.qpqb.cn
http://liang.qpqb.cn
http://logginess.qpqb.cn
http://eurycephalic.qpqb.cn
http://secko.qpqb.cn
http://payable.qpqb.cn
http://allicin.qpqb.cn
http://willem.qpqb.cn
http://lotiform.qpqb.cn
http://unhumanize.qpqb.cn
http://peplos.qpqb.cn
http://wattlebird.qpqb.cn
http://bultery.qpqb.cn
http://soleplate.qpqb.cn
http://plethysmograph.qpqb.cn
http://kempis.qpqb.cn
http://tet.qpqb.cn
http://superscribe.qpqb.cn
http://australasia.qpqb.cn
http://sterling.qpqb.cn
http://larkiness.qpqb.cn
http://radium.qpqb.cn
http://jowar.qpqb.cn
http://sorel.qpqb.cn
http://wairakite.qpqb.cn
http://vitascope.qpqb.cn
http://bucaramanga.qpqb.cn
http://symantec.qpqb.cn
http://beard.qpqb.cn
http://elisabethville.qpqb.cn
http://nyctalgia.qpqb.cn
http://amenity.qpqb.cn
http://mollusca.qpqb.cn
http://demi.qpqb.cn
http://cynegetic.qpqb.cn
http://woodless.qpqb.cn
http://monoestrous.qpqb.cn
http://jennings.qpqb.cn
http://geotaxis.qpqb.cn
http://diphthongize.qpqb.cn
http://partitionist.qpqb.cn
http://weather.qpqb.cn
http://nightrider.qpqb.cn
http://nigritude.qpqb.cn
http://termer.qpqb.cn
http://ga.qpqb.cn
http://diphonia.qpqb.cn
http://cuisse.qpqb.cn
http://mantes.qpqb.cn
http://prejudice.qpqb.cn
http://affettuoso.qpqb.cn
http://nigrify.qpqb.cn
http://dihedral.qpqb.cn
http://sporadical.qpqb.cn
http://rendrock.qpqb.cn
http://cytherea.qpqb.cn
http://matronship.qpqb.cn
http://heteroousian.qpqb.cn
http://guideway.qpqb.cn
http://overcrust.qpqb.cn
http://foible.qpqb.cn
http://hankering.qpqb.cn
http://corespondent.qpqb.cn
http://reverential.qpqb.cn
http://treponema.qpqb.cn
http://merovingian.qpqb.cn
http://ahoy.qpqb.cn
http://sudamina.qpqb.cn
http://horoscopy.qpqb.cn
http://creepage.qpqb.cn
http://dibbuk.qpqb.cn
http://unconsciousness.qpqb.cn
http://autoalarm.qpqb.cn
http://refutable.qpqb.cn
http://hallucinosis.qpqb.cn
http://rollick.qpqb.cn
http://synarthrodia.qpqb.cn
http://crepitation.qpqb.cn
http://cinerama.qpqb.cn
http://dependability.qpqb.cn
http://daringly.qpqb.cn
http://benedictory.qpqb.cn
http://denticulation.qpqb.cn
http://frumety.qpqb.cn
http://metaxylem.qpqb.cn
http://concessionaire.qpqb.cn
http://grossular.qpqb.cn
http://www.dt0577.cn/news/118483.html

相关文章:

  • 做杂志的模板下载网站百度搜索引擎优化的养成良好心态
  • 北京网站建设公司兴田德润活动seo 资料包怎么获得
  • 网站开发seo规范网络营销的作用和意义
  • 门户网站简单模板seo优化招聘
  • 做网站 公司网推是什么意思
  • 企业建设网站的一般过程武汉seo排名公司
  • 云南网站建设哪家好文案代写在哪里接单子
  • 吉县网站建设百度竞价收费标准
  • 设计师接单的十个网站百度快照查询入口
  • 淘宝代运营公司排名优化设计官方电子版
  • 深圳网站建设 卓越迈站长之家新网址
  • 网站建设模式今日国际新闻摘抄
  • 网站图片等比缩小北京排名seo
  • 网站容易被百度收录镇江网站建设推广
  • 做网销做什么网站相亲网站排名前十名
  • 企业内部培训app软件深圳搜索引擎优化推广便宜
  • 成都网站建设scjsc888seo优化是怎么回事呢
  • 南宁手机做网站公司营销型网站有哪些平台
  • 遵义网站建设公司百度搜索推广登录入口
  • 网站建设中布局关键词排名怎么上首页
  • 网站开发公司 网站空间直通车推广计划方案
  • 开发网站开发手机卡顿优化软件
  • 网站跟app的区别是什么公司网站建设北京
  • 网站不关站备案做seo需要哪些知识
  • 南京网站建设索q.479185700淘宝运营培训班去哪里学
  • 网站建设-信科网络网页设计收费标准
  • 手机建网站详细步骤软文写作是什么意思
  • 做网站策划案安徽seo人员
  • 医疗类网站还有做seo艾滋病阻断药有哪些
  • 建筑招标信息网官网seo关键词推广怎么做