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的更多相关文章

  1. [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, ...

  2. 102. Binary Tree Level Order Traversal 广度优先遍历

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  3. 【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 ...

  4. [刷题] 102 Binary Tree Level Order Traversal

    要求 对二叉树进行层序遍历 实现 返回结果为双重向量,对应树的每层元素 队列的每个元素是一个pair对,存树节点和其所在的层信息 1 Definition for a binary tree node ...

  5. 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, ...

  6. [LeetCode]题解(python):102 Binary Tree Level Order Traversal

    题目来源 https://leetcode.com/problems/binary-tree-level-order-traversal/ Given a binary tree, return th ...

  7. 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, ...

  8. 【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, ...

  9. 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 ...

随机推荐

  1. hook键盘钩子 带dll

    library Key; uses SysUtils, Classes, HookKey_Unit in 'HookKey_Unit.pas'; {$R *.res} exports HookOn,H ...

  2. String StringBuffer和StringBuilder的区别和联系

    1:String,StringBuffer和StringBuilder概念 1.1:String String中使用字符串数组来存储字符串,但是是fianl来修饰的,所以String的内容不可改变. ...

  3. (转载)Tomcat 报错 (The tomcat server configuration at /Servers/Tomcat v7.0 Server at localhost-config is mi)

    错误如图所示: 目前对于这个错误的原因尚不清楚,目前只知道如何解决这个错误,等到以后知道了原因之后再更改此文. 原因猜测: 之前你的eclipse关联的tomcat由于某种原因出现了信息丢失,需要重新 ...

  4. 干货 | 把Flutter扩展到微信小程序端的探索

    Google Flutter是一个非常优秀的跨端框架,不仅可以运行在Android. iOS平台,而且可以支持Web和桌面应用.在国内小程序是非常重要的技术平台,我们也一直思考能否把Flutter扩展 ...

  5. Centos7下yum安装软件报错解决办法

    Traceback (most recent call last): File "/usr/bin/yum", line 29, in yummain.user_main(sys. ...

  6. 布局基础<kotlin>(整理自网络)

    全屏 主界面 底部导航,bottombar 添加依赖 implementation 'com.roughike:bottom-bar:2.3.1' 主界面布局 <com.roughike.bot ...

  7. [极客大挑战 2019]PHP

    0x00知识点 1:直接扫描目录得到网站源码. 2:public.protected与private在序列化时的区别 protected 声明的字段为保护字段,在所声明的类和该类的子类中可见,但在该类 ...

  8. javaweb02

    第一个web服务器程序:开发部署到Tomcat服务器下运行 1).在eclipse新建一个Javaproject2).在java项目下创建web开发的目录结构 -Webcontent -WEB-INF ...

  9. LINUX的ssh免密码配置

    本篇将介绍LINUX的ssh免密码配置. 本篇中的有三台主机: 192.168.1.110 master.com.cn  主机192.168.1.111 salver1.com.cn192.168.1 ...

  10. 线性可分支持向量机--SVM(1)

    线性可分支持向量机--SVM (1) 给定线性可分的数据集 假设输入空间(特征向量)为,输出空间为. 输入 表示实例的特征向量,对应于输入空间的点: 输出 表示示例的类别. 线性可分支持向量机的定义: ...