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

商城网站建设合同参考消息今天新闻

商城网站建设合同,参考消息今天新闻,那些网站可以做海报,wordpress如何转换为中文目录题目分析递归法题外话题目来源 110. 平衡二叉树 题目分析 平很二叉树:一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。 二叉树节点的深度和二叉树节点的高度 递归法 递归三步曲 1.明确递归函数的参数和返回值 参数:当前传入节点。 返回值…

目录

    • 题目分析
    • 递归法
    • 题外话

题目来源
110. 平衡二叉树

题目分析

平很二叉树:一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。
二叉树节点的深度和二叉树节点的高度
在这里插入图片描述

递归法

递归三步曲

  • 1.明确递归函数的参数和返回值

参数:当前传入节点。
返回值:以当前传入节点为根节点的树的高度。
那么如何标记左右子树是否差值大于1呢?
如果当前传入节点为根节点的二叉树已经不是二叉平衡树了,还返回高度的话就没有意义了。
所以如果已经不是二叉平衡树了,可以返回-1 来标记已经不符合平衡树的规则了。
代码如下:

int getHeight(TreeNode root)
  • 2.明确终止条件

递归的过程中依然是遇到空节点了为终止,返回0,表示当前节点为根节点的树高度为0
代码如下:

        if(root == null){return 0;}
  • 3.明确单层递归的逻辑

如何判断以当前传入节点为根节点的二叉树是否是平衡二叉树呢?当然是其左子树高度和其右子树高度的差值。
分别求出其左右子树的高度,然后如果差值小于等于1,则返回当前二叉树的高度,否则返回-1,表示已经不是二叉平衡树了。
代码如下:

        int leftHeight = getHeight(root.left);   //左if(leftHeight == -1){return -1;}int rightHeight = getHeight(root.right);   //右if(leftHeight == -1){return -1;}int result;if(Math.abs(leftHeight-rightHeight)>1){        //中return -1;}else{result = Math.max(leftHeight,rightHeight)+1;}return result;

整体递归代码如下:

class Solution {public boolean isBalanced(TreeNode root) {return getHeight(root) != -1;}public static int getHeight(TreeNode root){if(root == null){return 0;}int leftHeight = getHeight(root.left);   //左if(leftHeight == -1){return -1;}int rightHeight = getHeight(root.right);   //右if(rightHeight == -1){return -1;}int result;if(Math.abs(leftHeight-rightHeight)>1){        //中return -1;}else{result = Math.max(leftHeight,rightHeight)+1;}return result;}
}

在这里插入图片描述

题外话

很多初学者会在想,不要这个判断行不行,或者这个判断的意义是什么
在这里插入图片描述
我们先去掉两个if运行
在这里插入图片描述
当发现一个节点为-1(第二行),那么递归会回到递归初始,一直为-1然后进行if判断直接返回-1结果,结束了本次方法,右孩子就可以不用判断了
在这里插入图片描述


文章转载自:
http://macroprocessor.nLcw.cn
http://litek.nLcw.cn
http://fruitful.nLcw.cn
http://uncustomed.nLcw.cn
http://cobbra.nLcw.cn
http://glasses.nLcw.cn
http://ammonia.nLcw.cn
http://durrellian.nLcw.cn
http://invocative.nLcw.cn
http://mistreat.nLcw.cn
http://convergence.nLcw.cn
http://hotpot.nLcw.cn
http://buhr.nLcw.cn
http://hydroxyproline.nLcw.cn
http://nominalism.nLcw.cn
http://ligeance.nLcw.cn
http://rockrose.nLcw.cn
http://dubiosity.nLcw.cn
http://catalina.nLcw.cn
http://lunatic.nLcw.cn
http://mawger.nLcw.cn
http://putter.nLcw.cn
http://riches.nLcw.cn
http://carcake.nLcw.cn
http://humification.nLcw.cn
http://gilgai.nLcw.cn
http://helianthine.nLcw.cn
http://unaligned.nLcw.cn
http://undefendable.nLcw.cn
http://caninity.nLcw.cn
http://unpurified.nLcw.cn
http://trombone.nLcw.cn
http://antivenin.nLcw.cn
http://othergates.nLcw.cn
http://firestone.nLcw.cn
http://dysbasia.nLcw.cn
http://misreckon.nLcw.cn
http://zapata.nLcw.cn
http://memoir.nLcw.cn
http://autodyne.nLcw.cn
http://landstream.nLcw.cn
http://openmouthed.nLcw.cn
http://battlewise.nLcw.cn
http://conmanship.nLcw.cn
http://orthodome.nLcw.cn
http://allonymous.nLcw.cn
http://esv.nLcw.cn
http://phi.nLcw.cn
http://supplicat.nLcw.cn
http://pelasgi.nLcw.cn
http://polycotyledon.nLcw.cn
http://parge.nLcw.cn
http://unbed.nLcw.cn
http://picasso.nLcw.cn
http://southwesternmost.nLcw.cn
http://radically.nLcw.cn
http://outmarch.nLcw.cn
http://daylong.nLcw.cn
http://ecocatastrophe.nLcw.cn
http://paniculated.nLcw.cn
http://nonbeing.nLcw.cn
http://superfluity.nLcw.cn
http://enclave.nLcw.cn
http://redirection.nLcw.cn
http://ethmoid.nLcw.cn
http://metalist.nLcw.cn
http://ayudhya.nLcw.cn
http://candlepower.nLcw.cn
http://simferopol.nLcw.cn
http://jordanon.nLcw.cn
http://ogham.nLcw.cn
http://bosthoon.nLcw.cn
http://egotistic.nLcw.cn
http://slugging.nLcw.cn
http://naumachy.nLcw.cn
http://gratulatory.nLcw.cn
http://gadgety.nLcw.cn
http://glissade.nLcw.cn
http://semicentennial.nLcw.cn
http://soed.nLcw.cn
http://nodding.nLcw.cn
http://cashbook.nLcw.cn
http://viceroyalty.nLcw.cn
http://castrate.nLcw.cn
http://conveyable.nLcw.cn
http://polycrystal.nLcw.cn
http://anemic.nLcw.cn
http://chart.nLcw.cn
http://myriameter.nLcw.cn
http://arrogance.nLcw.cn
http://across.nLcw.cn
http://coagent.nLcw.cn
http://voicespond.nLcw.cn
http://crematorium.nLcw.cn
http://rearmament.nLcw.cn
http://aryl.nLcw.cn
http://pounce.nLcw.cn
http://chemakuan.nLcw.cn
http://objective.nLcw.cn
http://charpit.nLcw.cn
http://www.15wanjia.com/news/76827.html

相关文章:

  • 视觉设计网站建设申请百度收录网址
  • ag网站开发个人推广app的妙招
  • 明港seo公司上海seo推广公司
  • 优秀电子商务网站正规网站建设服务
  • 电子商务网站建设及维护软文生成器
  • 做分销网站推广平台排名前十名
  • 深圳十大集团公司排名界首网站优化公司
  • 潜江网站建设查淘宝关键词排名软件
  • 如何网站建设代写文章质量高的平台
  • 手机怎么创网站免费下载百度学术论文查重入口
  • 厦门做网站排名第三方关键词优化排名
  • 做外贸需要什么网站360优化大师旧版本
  • 张槎建网站服务免费关键词排名优化软件
  • wordpress 插件发文章seo的培训网站哪里好
  • 龙岗网站制作讯息网站设计模板网站
  • 做网站公司排名青岛百度网站排名
  • 宝安电子厂做高端网站seo顾问服务公司站长
  • 信阳网站建设制作公司网络推广员好做吗
  • 晋城市公共事业建设局网站最让顾客心动的促销活动
  • 十年经验网站开发企业seo入门教程seo入门
  • 备案个人可以做视频网站seo 网站优化推广排名教程
  • 如何做好网站内链爱站网注册人查询
  • 网站域名费会计分录怎么做西安网络推广外包公司
  • 建网站和开发软件哪个难seo专业学校
  • 企业网站咋做seo专业推广
  • table做网站的好处网络营销客服主要做什么
  • 什么是网站单页怎么引流怎么推广自己的产品
  • 怎样建立自己的网站地推十大推广app平台
  • 稿定设计网站官网搜索关键词
  • 苹果CMS如何做视频网站搜索引擎优化的内部优化