[Leetcode][JAVA] 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,#,#,15,7}
,
- 3
- / \
- 9 20
- / \
- 15 7
return its level order traversal as:
- [
- [3],
- [9,20],
- [15,7]
- ]
- 对每一层的节点都用ArrayList<TreeNode> cur保存,每次循环时把cur里每个非空节点值拿出来并保存到ArrayList<Integer> temp中, 并且子节点保存到新的ArrayList<TreeNode>中。最后如果 temp 非空,就加入到结果集中。最后cur 指向新的ArrayList<TreeNode>。
- public List<List<Integer>> levelOrder(TreeNode root) {
- List<List<Integer>> re = new ArrayList<List<Integer>>();
- List<TreeNode> lst = new ArrayList<TreeNode>();
- lst.add(root);
- while(lst.size()>0){
- List<Integer> temp = new ArrayList<Integer>();
- List<TreeNode> childrenlst = new ArrayList<TreeNode>();
- for(int i=0;i<lst.size();i++){
- TreeNode curNode = lst.get(i);
- if(curNode!=null){
- temp.add(lst.get(i).val);
- childrenlst.add(curNode.left);
- childrenlst.add(curNode.right);
- }
- }
- if(temp.size()>0)
- re.add(temp);
- lst = childrenlst;
- }
- return re;
- }
Level Order Traverse II 就是反一反。
[Leetcode][JAVA] Binary Tree Level Order Traversal的更多相关文章
- Java for LeetCode 107 Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)
翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...
- [LeetCode] 107. Binary Tree Level Order Traversal II 二叉树层序遍历 II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- LeetCode(32)-Binary Tree Level Order Traversal
题目: LeetCode Premium Subscription Problems Pick One Mock Articles Discuss Book fengsehng 102. Binary ...
- [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】Binary Tree Level Order Traversal I & II
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树
Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its ...
- (二叉树 BFS) leetcode 107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- Java for 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, ...
随机推荐
- 可以让电脑卡机的c++程序
#include <iostream> #include<windows.h> #include <shellapi.h> #include <stdio.h ...
- Ruby Unit Test
require 'test/unit' module Test::Unit::Assertions def assert_equal_unorder(exp, act, msg = nil) # ms ...
- 02 Apache Solr: 概览 Solr在信息系统架构中的位置
概述: Apache Solr是一个用JAVA语言构建在Apache Lucene项目上的开源的企业级搜索平台.主要特性包含:全文搜索.命中高亮.片段式搜索.实时索引.动态集群.数据库集成. ...
- SikuliLibrary 库关键字注释
在 https://github.com/rainmanwy/robotframework-SikuliLibrary 看到rainmanwy 整理的SikuliLibrary库,非常适合工作需要, ...
- 移动APP开发使用什么样的原型设计工具比较合适?
原型设计工具有Axure,Balsamiq Mockups,JustinMind,iClap原型工具,等其他原型工具.其中JustinMind比较适合APP开发使用. JustinMind可以输出Ht ...
- IOS中无缓存的图片载入
在IOS中,我们常用[UIImage imageNamed]方法获取图像,这种方法简便,容易理解.但是有个缺点,就是有缓存.这种方式 传人的图像的就是通过文件名方式文件名.如果,我们内存有限,我们就必 ...
- 详解C语言的类型转换
1.自动类型转换 字符型变量的值实质上是一个8位的整数值,因此取值范围一般是-128-127,char型变量也可以加修饰符unsigned,则unsigned char 型变量的取值范围是0-255( ...
- DataGridView in TabControl and CellValidating lead to problems
I created a little form with a TabControl on it and a combobox. On the first page i added a DataGri ...
- wpf 后台绘制圆弧
wpf 前台绘制圆弧很简单,如:<Path x:Name="path_data" Stroke="#FFE23838" StrokeThickness=& ...
- seaJS 简单例子,理解seaJS
学习心得: 记得第一次学underscore的时候,去的官网(不管什么都是官网好),呼啦一长列语法,我就一个个看,看完也不知道underscore是做什么的.就是现在underscore我也用不上,学 ...