[LC] 102. Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree [3,9,20,null,null,15,7]
,
3
/ \
9 20
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
]
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> arrList = new ArrayList<>();
helper(arrList, 0, root);
return arrList;
} private void helper(List<List<Integer>> res, int depth, TreeNode root) {
if (root == null) {
return;
} if (depth >= res.size()) {
res.add(new LinkedList<>());
}
res.get(depth).add(root.val);
helper(res, depth + 1, root.left);
helper(res, depth + 1, root.right);
}
}
[LC] 102. Binary Tree Level Order Traversal的更多相关文章
- [LeetCode] 102. Binary Tree Level Order Traversal 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 102. Binary Tree Level Order Traversal 广度优先遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 【LeetCode】102. Binary Tree Level Order Traversal (2 solutions)
Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...
- [刷题] 102 Binary Tree Level Order Traversal
要求 对二叉树进行层序遍历 实现 返回结果为双重向量,对应树的每层元素 队列的每个元素是一个pair对,存树节点和其所在的层信息 1 Definition for a binary tree node ...
- leetcode 102. Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- [LeetCode]题解(python):102 Binary Tree Level Order Traversal
题目来源 https://leetcode.com/problems/binary-tree-level-order-traversal/ Given a binary tree, return th ...
- leetcode 102 Binary Tree Level Order Traversal ----- java
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 【LeetCode】102 - Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- Java [Leetcode 102]Binary Tree Level Order Traversal
题目描述: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ...
随机推荐
- 直击JDD | 共建智能新城 京东云让城市生活变得简单美好
技术快速革新,创新持续激发.在"智能+"时代,云计算.大数据.5G等新技术,已成为社会生产方式变革.创新人类生活空间的重要力量--11月19日,JDD-2019京东全球科技探索者大 ...
- Java线程——线程池概念
什么是线程池? 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了,这样频繁创建线程就会大大降低系统的效率,因为频繁创建线程和销毁线程需要时间.那么有没有一种办法使得线程可以复用, ...
- pymysql常见报错
错误一: AttributeError: module 'pymysql' has no attribute 'connect' 有道翻译 AttributeError:模块'pymysql'没有属性 ...
- PowerDesigner 表格导出为excel(转载)
选中tablesctrl + shift +x 然后运行脚本 '******************************************************************** ...
- ubuntu服务器上配置tomcat
前言 嗯,最近想在自己的腾讯云服务器上跑个项目玩玩,由于服务器是重装的系统,所以,只能自己手动装tomcat. 不过,tomcat是基于java的,必须又java环境tomcat才能够使用,因此首先要 ...
- Java 语句while、do while、for循环、嵌套、for与while的区别、break continue(3)
for循环语句,双从for嵌套: /* for(初始化表达式:循环条件表达式:循环后的操作表达式) { 执行语句: } */ /*1,变量有自己的作用域.对于for来讲:如果将用于控制循环的增量定义在 ...
- 手机H5,用Jquery使图片自动填满两栏式排版
遇上这样的排版,手机的解象度都不同,假如只用CSS3根本就做不出这样的排版:因此要用Jquery. 1. HTML <div class="postImgCenterCrop" ...
- php的执行流程
源代码(人认识)->字节码(解释器认识)->机器码(硬件认识)来看下PHP的执行流程,假设有个a.php文件,不启用opacache的流程如下:a.php->经过zend编译-> ...
- Django知识点集合
- MobX 在 hook 中的使用
关于 mobX 在 react 16.8.0 以上的用法 以下例子均取自官网文档 一般用法: import { observer, useLocalStore } from 'mobx-react'; ...