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

【CT】LeetCode手撕—102. 二叉树的层序遍历

目录

  • 题目
  • 1-思路
  • 2- 实现
    • ⭐102. 二叉树的层序遍历——题解思路
  • 3- ACM实现
    • 3-1 二叉树构造
    • 3-2 整体实现


题目

  • 原题连接:102. 二叉树的层序遍历

1-思路

  • 1.借助队列 Queue ,每次利用 ①while 循环遍历当前层结点②将当前层结点的下层结点放入 Queue中
  • 2.每遍历一个结点,将值收集到 iterm 中,每一层遍历完,将结果存到 res

2- 实现

⭐102. 二叉树的层序遍历——题解思路

在这里插入图片描述

class Solution {// 结果List<List<Integer>> res = new ArrayList<>();public List<List<Integer>> levelOrder(TreeNode root) {Queue<TreeNode> queue = new LinkedList<>();if(root==null){return res;}queue.offer(root);while(!queue.isEmpty()){int len = queue.size();List<Integer> iterm = new ArrayList<>();while(len>0){TreeNode node = queue.poll();iterm.add(node.val);if(node.left!=null){queue.offer(node.left);}if(node.right!=null){queue.offer(node.right);}len--;}res.add(new ArrayList(iterm));}return res;}
}

3- ACM实现

3-1 二叉树构造

在这里插入图片描述

public static TreeNode build(Integer[] nums){// 借助 queue 来实现二叉树构造Queue<TreeNode> queue = new LinkedList<>();TreeNode root = new TreeNode(nums[0]);queue.offer(root);int index = 1;while (!queue.isEmpty() && index < nums.length){TreeNode node = queue.poll();if(nums[index]!=null && index<nums.length){node.left = new TreeNode(nums[index]);queue.offer(node.left);}index++;if (nums[index]!=null && index<nums.length){node.right = new TreeNode(nums[index]);queue.offer(node.right);}index++;}return root;
}

3-2 整体实现

public class levelTraversal {static class TreeNode{int val;TreeNode left;TreeNode right;TreeNode(){}TreeNode(int x){val = x;}}public static TreeNode build(Integer[] nums){// 借助 queue 来实现二叉树构造Queue<TreeNode> queue = new LinkedList<>();TreeNode root = new TreeNode(nums[0]);queue.offer(root);int index = 1;while (!queue.isEmpty() && index < nums.length){TreeNode node = queue.poll();if(nums[index]!=null && index<nums.length){node.left = new TreeNode(nums[index]);queue.offer(node.left);}index++;if (nums[index]!=null && index<nums.length){node.right = new TreeNode(nums[index]);queue.offer(node.right);}index++;}return root;}static List<List<Integer>> res = new ArrayList<>();public static List<List<Integer>> levelOrder(TreeNode root) {Queue<TreeNode> queue = new LinkedList<>();if(root==null){return res;}queue.offer(root);while(!queue.isEmpty()){int len = queue.size();List<Integer> iterm = new ArrayList<>();while(len>0){TreeNode node = queue.poll();iterm.add(node.val);if(node.left!=null){queue.offer(node.left);}if(node.right!=null){queue.offer(node.right);}len--;}res.add(new ArrayList(iterm));}return res;}public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("输入二叉树构造数组");String input = sc.nextLine();input = input.replace("[","");input = input.replace("]","");String[] parts = input.split(",");Integer[] nums = new Integer[parts.length];for(int i = 0 ; i <parts.length;i++){if(!parts[i].equals("null")){nums[i] = Integer.parseInt(parts[i]);}else{nums[i] = null;}}TreeNode root = build(nums);List<List<Integer>> forRes = levelOrder(root);for(List<Integer> i:forRes){System.out.println(i.toString());}}}

相关文章:

  • 如何查看当前的gruop_id 的kafka 消费情况 这个可以查看到是否存在消费阻塞问题
  • 记录:UA_Client_readValueAttribute 读取失败 C0错误码
  • RabbitMQ延迟消息(通过死信交换机实现)
  • 电子画册制作与传统画册相比,有哪些优势?
  • nc网络收发测试-tcp客户端\TCP服务器\UDP\UDP广播
  • 仿element-ui 实现自己组件库 <3>
  • 前端 JS 经典:Vue 状态仓库持久化
  • 24年河北自考报名流程详细教程汇总
  • Python实战:小说分词统计-数据可视化
  • 前端框架之 MVVM
  • 金融与大模型:引领行业未来的创新融合
  • 6spark期末复习
  • 探索 Spring Boot 集成缓存功能的最佳实践
  • Ajax的应用
  • vue+elementUI实现在表格中添加输入框并校验的功能
  • 08.Android之View事件问题
  • 2019.2.20 c++ 知识梳理
  • 30天自制操作系统-2
  • ES2017异步函数现已正式可用
  • es6要点
  • Linux编程学习笔记 | Linux IO学习[1] - 文件IO
  • MobX
  • mysql innodb 索引使用指南
  • Python学习笔记 字符串拼接
  • TypeScript迭代器
  • Vue.js-Day01
  • 闭包--闭包作用之保存(一)
  • 程序员该如何有效的找工作?
  • 持续集成与持续部署宝典Part 2:创建持续集成流水线
  • 基于 Babel 的 npm 包最小化设置
  • 开放才能进步!Angular和Wijmo一起走过的日子
  • 开源中国专访:Chameleon原理首发,其它跨多端统一框架都是假的?
  • 跨域
  • 漂亮刷新控件-iOS
  • 区块链分支循环
  • 如何胜任知名企业的商业数据分析师?
  • 使用parted解决大于2T的磁盘分区
  • 收藏好这篇,别再只说“数据劫持”了
  • 首页查询功能的一次实现过程
  • 宾利慕尚创始人典藏版国内首秀,2025年前实现全系车型电动化 | 2019上海车展 ...
  • ​3ds Max插件CG MAGIC图形板块为您提升线条效率!
  • # 职场生活之道:善于团结
  • ######## golang各章节终篇索引 ########
  • $con= MySQL有关填空题_2015年计算机二级考试《MySQL》提高练习题(10)
  • $分析了六十多年间100万字的政府工作报告,我看到了这样的变迁
  • ( 用例图)定义了系统的功能需求,它是从系统的外部看系统功能,并不描述系统内部对功能的具体实现
  • (04)odoo视图操作
  • (11)MSP430F5529 定时器B
  • (160)时序收敛--->(10)时序收敛十
  • (4)(4.6) Triducer
  • (C++20) consteval立即函数
  • (Matalb时序预测)WOA-BP鲸鱼算法优化BP神经网络的多维时序回归预测
  • (待修改)PyG安装步骤
  • (二十一)devops持续集成开发——使用jenkins的Docker Pipeline插件完成docker项目的pipeline流水线发布
  • (附源码)spring boot儿童教育管理系统 毕业设计 281442