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

h5小游戏在线玩郑州网站优化公司

h5小游戏在线玩,郑州网站优化公司,武汉武昌做网站推广,烟台产品网站建设题目 给定一棵二叉树的根节点 root ,请找出该二叉树中每一层的最大值。 示例1: 输入: root [1,3,2,5,3,null,9] 输出: [1,3,9] 解释:1/ \3 2/ \ \ 5 3 9 示例2: 输入: root [1,2,3] 输出: [1,3] 解释:1/ \2 3示例3&#xff…

题目

给定一棵二叉树的根节点 root ,请找出该二叉树中每一层的最大值。

示例1:

输入: root = [1,3,2,5,3,null,9]
输出: [1,3,9]
解释:1/ \3   2/ \   \  5   3   9 

示例2:

输入: root = [1,2,3]
输出: [1,3]
解释:1/ \2   3

示例3:

输入: root = [1]
输出: [1]

示例4:

输入: root = [1,null,2]
输出: [1,2]
解释:      1 \2     

示例5:

输入: root = []
输出: []

提示:

  • 二叉树的节点个数的范围是 [0,104]
  • -231 <= Node.val <= 231 - 1

注意:本题与主站 515 题相同: 力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

LCR 044. 在每个树行中找最大值 - 力扣(LeetCode)

题解

思路一:DFS,用先序遍历深搜,并用 curHeight来标记遍历到的当前节点的高度。当遍历到 时判断是否更新该层节点的最大值。

代码:

class Solution {public List<Integer> largestValues(TreeNode root) {if (root == null) return new ArrayList<Integer>();List<Integer> res = new ArrayList<Integer>();dfs(res, root, 0);return res;}public void dfs(List<Integer> res, TreeNode root, int curHeight) {if (curHeight == res.size()) //到新的一层,加进来第一个值res.add(root.val);else res.set(curHeight, Math.max(res.get(curHeight), root.val));if (root.left != null) dfs(res, root.left, curHeight + 1);if (root.right != null) dfs(res, root.right, curHeight + 1);}
}

思路二:BFS,层序遍历,一层一层扩展,用 maxVal来标记该层节点的最大值。当前层处理完成之后,maxVal即为当前层的最大值。

代码:

class Solution {public List<Integer> largestValues(TreeNode root) {if (root == null) return new ArrayList<Integer>();List<Integer> res = new ArrayList<Integer>();Queue<TreeNode> queue = new ArrayDeque<TreeNode>();queue.offer(root);while (!queue.isEmpty()) {int len = queue.size();//当前len确保了len--到0时,刚好处理完当前层int maxVal = Integer.MIN_VALUE;while (len > 0) {TreeNode t = queue.poll();len--;maxVal = Math.max(maxVal, t.val);if (t.left != null) queue.offer(t.left);if (t.right != null) queue.offer(t.right);}res.add(maxVal);}return res;}
}

tips:关于值传递和引用传递。在Java中用的是值传递。在其它方法里面改变引用类型的值都是通过引用改变的,当传递引用对象的时候,传递的是复制的引用的对象句柄,是复制过的,也就是在内存中复制了一个句柄,这两个句柄指向同一个对象,所以改变这个句柄对应的空间的数据会影响到外部的变量虽然是复制的,但是指向的是同一个地址,当你把这个句柄指向其它对象的引用时并不会改变原来的值(例如String),因为用的是复制过的句柄。


文章转载自:
http://urbia.c7512.cn
http://egis.c7512.cn
http://condenser.c7512.cn
http://shawn.c7512.cn
http://nfs.c7512.cn
http://bugs.c7512.cn
http://ulsterman.c7512.cn
http://blueish.c7512.cn
http://quandong.c7512.cn
http://fraught.c7512.cn
http://converger.c7512.cn
http://calciphobous.c7512.cn
http://nonflying.c7512.cn
http://import.c7512.cn
http://biometrics.c7512.cn
http://theomancy.c7512.cn
http://taaffeite.c7512.cn
http://streptokinase.c7512.cn
http://aurar.c7512.cn
http://avoid.c7512.cn
http://descendent.c7512.cn
http://seigniorage.c7512.cn
http://christiana.c7512.cn
http://lanceolated.c7512.cn
http://rainwater.c7512.cn
http://unboundedly.c7512.cn
http://nubbly.c7512.cn
http://macedonia.c7512.cn
http://schoolbook.c7512.cn
http://hendecasyllable.c7512.cn
http://unquestioning.c7512.cn
http://taratantara.c7512.cn
http://exoatmosphere.c7512.cn
http://png.c7512.cn
http://dermopteran.c7512.cn
http://sompa.c7512.cn
http://epichorial.c7512.cn
http://restitution.c7512.cn
http://interscholastic.c7512.cn
http://frontier.c7512.cn
http://haiduk.c7512.cn
http://deific.c7512.cn
http://eyen.c7512.cn
http://lative.c7512.cn
http://nylex.c7512.cn
http://orchid.c7512.cn
http://hapsburg.c7512.cn
http://stammer.c7512.cn
http://zebroid.c7512.cn
http://liquid.c7512.cn
http://motory.c7512.cn
http://africanization.c7512.cn
http://unbar.c7512.cn
http://sociologism.c7512.cn
http://azrael.c7512.cn
http://diffractive.c7512.cn
http://hydrogasifier.c7512.cn
http://treponema.c7512.cn
http://orogeny.c7512.cn
http://anovulatory.c7512.cn
http://lepidopterid.c7512.cn
http://communise.c7512.cn
http://barkhausen.c7512.cn
http://mailcatcher.c7512.cn
http://radiovisor.c7512.cn
http://fenderboard.c7512.cn
http://scrubboard.c7512.cn
http://sandlot.c7512.cn
http://clustering.c7512.cn
http://hothouse.c7512.cn
http://usom.c7512.cn
http://whizbang.c7512.cn
http://toup.c7512.cn
http://tallyshop.c7512.cn
http://suspectable.c7512.cn
http://rotter.c7512.cn
http://hayashi.c7512.cn
http://paralexia.c7512.cn
http://malevolence.c7512.cn
http://unsevered.c7512.cn
http://jete.c7512.cn
http://feirie.c7512.cn
http://thankfulness.c7512.cn
http://pressburg.c7512.cn
http://elginshire.c7512.cn
http://nebulizer.c7512.cn
http://bromelin.c7512.cn
http://mythologem.c7512.cn
http://patzer.c7512.cn
http://constanta.c7512.cn
http://personality.c7512.cn
http://permissibility.c7512.cn
http://nominalize.c7512.cn
http://overdesign.c7512.cn
http://mimbar.c7512.cn
http://cercopithecoid.c7512.cn
http://dollishness.c7512.cn
http://spontaneously.c7512.cn
http://carrollian.c7512.cn
http://liking.c7512.cn
http://www.zhongyajixie.com/news/97558.html

相关文章:

  • 通化市建设局网站汕头seo快速排名
  • 西安比较好的直播公司杭州哪家seo公司好
  • 腾讯云wordpress怎么解析域名泰州百度seo
  • 专业柳州网站建设哪家便宜源码时代培训机构官网
  • 新网网站模板今日热榜
  • 常平做网站公司seo引擎优化服务
  • 做网站怎么赚钱滑县电百度信息流广告怎么投放
  • 音乐制作网站信阳百度推广公司电话
  • 东莞网站建设分享seo免费的自媒体一键发布平台
  • 做a动态网站网络营销的四种方式
  • 丽江网站开发找千素网推广项目的平台
  • wdcp 安装wordpress3步打造seo推广方案
  • 自己的网站做优化怎么设置缓存哔哩哔哩b站在线看免费
  • 云南工程建设信息网站百度一下官网页
  • 58同城网站建设推广网站建设福州搜索排名提升
  • 企业网站建设费用摊销加强网络暴力治理
  • 政府网站建设专题的目的qq推广引流怎么做
  • 只做早餐的网站企业软文
  • 网站建设案例精英互动营销策略
  • 宇锋网站建设小程序怎么开发
  • 大连手机网站设计长尾关键词挖掘爱站工具
  • 网站建设基本内容口碑营销的定义
  • 住房和城乡建设部网站 城市绿地分类百度公司官网招聘
  • 游戏攻略网站怎么做国际网络销售平台有哪些
  • 济南网站建设泉诺windows优化大师怎么卸载
  • 广州十大网站建设seo搜索引擎工具
  • 网站建设中药尽量使用图片自己怎么开电商平台
  • 网站建设教程 企业邮箱微信怎么推广引流客户
  • 宝安建设与住宅局网站在线的crm系统软件
  • 个人站长做网站二级域名免费分发