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]
]

Best approach:

 class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<> ();
if (root == null) return res;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
List<Integer> row = new ArrayList<>();
for (int i = 0; i < size; i ++) {
TreeNode node = queue.poll();
row.add(node.val);
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
res.add(row);
}
return res;
}
}

把树看成一个有向图,进行BFS。BST的通常做法都是维护一个队列。这道题的难度在于,如何在队列不断的入队出队操作中,确定哪些node是一个层次的。想了很久没什么好办法,参考了网上的做法,发现他在维护两个数:一个是父节点所在层次的节点在当前队列中的数目ParentNumInQ,另一个是子节点所在层次的节点在当前队列中的数目ChildNumInQ。初始数值为1和0. 每次父节点出队,ParentNumInQ--,每次子节点入队,ParentNumInQ++。一旦ParentNumInQ减至0,说明当前父节点层次已全部出队,全部被存入LinkedList中,这就得到了一层的所有节点。接下来需要一些更新,ParentNumInQ = ChildNumInQ, ChildNumInQ = 0开始考虑下一层。

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) {
ArrayList<ArrayList<Integer>> lists = new ArrayList<ArrayList<Integer>> ();
if (root == null) return lists;
LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
int ParentNumInQ = 1;
int ChildNumInQ = 0;
ArrayList<Integer> list = new ArrayList<Integer>();
while (!queue.isEmpty()) {
TreeNode cur = queue.poll();
list.add(cur.val);
ParentNumInQ--;
if (cur.left != null) {
queue.add(cur.left);
ChildNumInQ++;
}
if (cur.right != null) {
queue.add(cur.right);
ChildNumInQ++;
}
if (ParentNumInQ == 0) {
ParentNumInQ = ChildNumInQ;
ChildNumInQ = 0;
lists.add(list);
list = new ArrayList<Integer>();
}
}
return lists;
}
}

Leetcode: Binary Tree Level Order Transversal的更多相关文章

  1. Leetcode: Binary Tree Level Order Transversal II

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

  2. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  3. [LeetCode] Binary Tree Level Order Traversal II 二叉树层序遍历之二

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

  4. [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历

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

  5. [leetcode]Binary Tree Level Order Traversal II @ Python

    原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题意: Given a binary tree, ...

  6. LeetCode: Binary Tree Level Order Traversal 解题报告

    Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...

  7. [Leetcode] Binary tree level order traversal ii二叉树层次遍历

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

  8. [LeetCode] Binary Tree Level Order Traversal 与 Binary Tree Zigzag Level Order Traversal,两种按层次遍历树的方式,分别两个队列,两个栈实现

    Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...

  9. LeetCode——Binary Tree Level Order Traversal II

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

随机推荐

  1. Android SDK更新8.1.0时报错

    Done loading packages.Preparing to install archivesDownloading SDK Platform Android 8.1.0, API 27, r ...

  2. LeetCode 27 Remove Element (移除数组中指定元素)

    题目链接: https://leetcode.com/problems/remove-element/?tab=Description   Problem : 移除数组中给定target的元素,返回剩 ...

  3. C语言assert()函数用法总结

    assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行,原型定义: #include <assert.h> void assert( i ...

  4. facebook login issue

    If enable the facebook account in settings, when change account can't open the session. -(void)fbRes ...

  5. 剖析Elasticsearch集群系列之一:Elasticsearch的存储模型和读写操作

    转载:http://www.infoq.com/cn/articles/analysis-of-elasticsearch-cluster-part01 1.辨析Elasticsearch的索引与Lu ...

  6. 用ELK打造可视化集中式日志

    原文链接:https://yq.aliyun.com/articles/57420 摘要: Elk是Elastic search, Logstash和Kibana三者的简称. Elastic sear ...

  7. 【转】python中json.loads与eval的区别

    JSON有两种结构: “名称/值”对的集合(A collection of name/value pairs).不同的语言中,它被理解为对象(object),纪录(record),结构(struct) ...

  8. codeforces 798B - Mike and strings

    感觉自己好咸鱼呀……B题写了这么久,虽然可以算作1A(忽略一次少include一个头文件的CE)…… 思想很简单,每次选定一个字符串作为目标字符串,然后把其他所有字符串都当做测试字符串,计算出总共需要 ...

  9. 2018No-java面试知识

    1.框架 1. springboot比spring的优点? 2. Springmvc的基本流程? 3. 微服务之间调用不会慢吗? 4. 大图片和大数据库怎么存储? 5. spring事物?四大特征, ...

  10. Kettle 4.2源码分析第二讲--Kettle插件结构体系简介

    1.  插件体系结构 1.1. 插件技术原理 1.1.1.    插件概念说明 插件是一种遵循统一的预定义接口规范编写出来的程序,应用程序在运行时通过接口规范对插件进行调用,以扩展应用程序的功能.在英 ...