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. filter的原理(转)

    今天学习了一下javaweb开发中的Filter技术,于是在网上搜了一下相关资料,发现这篇博客写的很不错,于是希望能转载过来以备以后继续学习之用.(原:http://www.cnblogs.com/x ...

  2. XML文件读写编码不是UTF-8的问题

    FileWriter和FileReader在写.读文件时,使用系统当前默认的编码方式. 在中文win下encoding基本是GB2312,在英文win下基本是ISO-8859-1.所以要创建一个UTF ...

  3. 72)MFC测试动态共享库

    动态共享库: 首先我建立一个新的动态库: 然后不选择空项目了,因为我们普通的cpp文件 入口是main  win32入口是winmain  那么这个动态库的入口在哪里  我们就是为了看一看: 出来这样 ...

  4. Python—程序设计:单例模式

    单例模式 单例模式(Singleton Pattern)属于创建型模式,它提供了一种创建对象的最佳方式.这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建,并提供一种访问其 ...

  5. Maven - No plugin found for prefix 'tomcat7' in the current project

    问题发现: 在构建Maven项目的时候,出现了No plugin found for prefix 'tomcat7' in the current project的错误. 是需要在Maven的Pom ...

  6. 可能对Flutter应用程序开发有用的代码/库/专有技术列表

    当我开始使用Flutter实施该应用程序时,我开始担心“如何最好地编写?”以及“如何使其更好地放置?”. 在这种情况下,您将需要参考GitHub上发布的代码和应用程​​序. 因此,我收集了似乎对Flu ...

  7. modbus 指令16 $10 的格式

    { //写多个请求 01(从设备)10(功能码) 00 77(起始地址) 00 01(寄存器数) 02(字节数) 05 55(写的数据) 6F B8(CRC) //写多个返回 01(从设备) 10(功 ...

  8. C#压缩解压zip 文件

    /// <summary> /// Zip 压缩文件 /// </summary> public class Zip { public Zip() { } #region 加压 ...

  9. 最大子矩阵和(二维矩阵转一维DP)

    题目描述 蒜头君拿到了一个矩阵,他想知道其中的最大非空子矩阵和是多少. 输入格式 第一行输入两个整数 n,m代表这个矩阵的行数和列数.接下来n行,每行m个整数 ai1,ai2,ai3⋯aim.(1≤m ...

  10. Insulator|enhancer|LCR|EKLF|CTCF|调控基因印记| A-USF|HATs|ChIP|Chip-seq|PAGE|

    表观遗传学 转录因子 基本转录因子:TFIID.A.B.F.E.H. Pol II… 基转录因子具有稳定作用 组织特异性转录因子:GATA.EKLF.Bcl11A… 特异性是在特定组织中的细胞中时与细 ...