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

DFS实现:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
// DFS 遍历
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
traverse(res, root, 0);
return res;
} public void traverse(List<List<Integer>> res, TreeNode root, int level) {
if (root == null) {
return;
}
if (res.size() > level) {
res.get(level).add(root.val);
} else {
ArrayList list = new ArrayList<Integer>();
list.add(root.val);
res.add(list);
}
traverse(res, root.left, level + 1);
traverse(res, root.right, level + 1);
} }

BFS实现:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
// BFS遍历打印
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new LinkedList<List<Integer>>();
if (root == null)
return res;
LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
ArrayList<Integer> list = new ArrayList<Integer>();
int cur = 1;
int nextLevel = 0;
while (!queue.isEmpty()) {
TreeNode tmp = queue.poll(); cur--;
if (tmp.left != null) {
queue.add(tmp.left);
nextLevel++;
}
if (tmp.right != null) {
queue.add(tmp.right);
nextLevel++;
}
list.add(tmp.val);
if (cur == 0) {
res.add(list);
list = new ArrayList<Integer>();
cur = nextLevel;
nextLevel = 0;
} }
return res;
} }

Binary Tree Level Order Traversal - leetcode - java的更多相关文章

  1. leetcode 107 Binary Tree Level Order Traversal II ----- java

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

  2. Binary Tree Level Order Traversal [LeetCode]

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

  3. leecode Binary Tree Level Order Traversal II java

    做完这道题,只能说基本功很重要,数组中套数组就不会用了,过几天吧1做了,看自己到底等没. https://oj.leetcode.com/problems/binary-tree-level-orde ...

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

  5. LeetCode 102. 二叉树的层次遍历(Binary Tree Level Order Traversal) 8

    102. 二叉树的层次遍历 102. Binary Tree Level Order Traversal 题目描述 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 每 ...

  6. [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 ...

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

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

  8. LeetCode:Binary Tree Level Order Traversal I II

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

  9. lettcode-102:Binary Tree Level Order Traversal (Java)

    Binary Tree Level Order Traversal 二叉树的层序遍历 两种方式: 1.用两个queue交替表示每一层的节点 2.用两个node,一个表示当前层的最后一个节点,一个表示下 ...

随机推荐

  1. 经过一年时间的沉淀 再次回首 TCP Socket服务器编程--转

    ------------------ 前言 ------------------ 开发了这么多年,发现最困难的程序开发就是通讯系统. 其他大部分系统,例如CRM/CMS/权限框架/MIS之类的,无论怎 ...

  2. 将Oracle JDBC驱动库安装到本地仓库

    1.为了添加详细版本信息,先查看当前Oracle数据库版本 select * from v$version; 查询返回如下信息 BANNER ----------------------------- ...

  3. Eight(South Central USA 1998)(八数码) 分类: bfs 2015-07-05 22:34 1人阅读 评论(0) 收藏

    The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've see ...

  4. libcurl 多线程使用注意事项 - Balder~专栏 - 博客频道 - CSDN.NET

    libcurl 多线程使用注意事项 - Balder~专栏 - 博客频道 - CSDN.NET libcurl 多线程使用注意事项 分类: C/C++学习 2012-05-24 18:48 2843人 ...

  5. hdu1166 树状数组

    不知道为什么用C++输入输出死活不过,换成C的就过了... #include <stdio.h> #include <string.h> //================= ...

  6. IP、路由配置

    IP地址组成: 网络地址主机地址32位二进制 A类: 255.0.0.0, 8:    0 000 0001 - 0 111 1111     127个A类,127用于回环,1-126    2^7- ...

  7. iOS SDK原生JSON解析

    - (IBAction)touchReadButton:(id)sender { NSData *jsonData = [[NSData alloc] initWithContentsOfFile:J ...

  8. 格而知之6:我所理解的Runtime(1)

    基本简介 1.根据官方文档,OC有一个特性:它会尽可能把一些决定从编译时和链接时推迟到运行时才处理,所以这门语言需要的就不只是一个编译器,它还需要一个runtime系统来处理那些已经被编译过的代码. ...

  9. mysql的常用操作的封装

    1.概述: 为了把繁琐的操作简化成简单的类,设计了2个类用来封装了mysql的常用操作,以便使用者可以方便地使用. 2.组成 1)数据库操作类CDatabaseConnect 2)SQL对象类CSql ...

  10. C++中如何建立一个顺序表

    准备数据 #define MAXLEN 100 //定义顺序表的最大长度 struct DATA { char key[10]; //结点的关键字 char name[20]; int age; }; ...