Binary Tree Level Order Traversal - leetcode - java
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的更多相关文章
- 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 ...
- Binary Tree Level Order Traversal [LeetCode]
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- leecode Binary Tree Level Order Traversal II java
做完这道题,只能说基本功很重要,数组中套数组就不会用了,过几天吧1做了,看自己到底等没. https://oj.leetcode.com/problems/binary-tree-level-orde ...
- 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 102. 二叉树的层次遍历(Binary Tree Level Order Traversal) 8
102. 二叉树的层次遍历 102. Binary Tree Level Order Traversal 题目描述 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 每 ...
- [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 ...
- [LeetCode] 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
LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...
- lettcode-102:Binary Tree Level Order Traversal (Java)
Binary Tree Level Order Traversal 二叉树的层序遍历 两种方式: 1.用两个queue交替表示每一层的节点 2.用两个node,一个表示当前层的最后一个节点,一个表示下 ...
随机推荐
- POJ 2533 Longest Ordered Subsequence - from lanshui_Yang
题目大意:求一个数列的最长上升子序列(严格上升). 解题思路: 方法一:O(n^2) dp[i]:表示处理到第i个位置,序列的最长上升子序列末尾为i的长度: a[]数组存储原序列 dp[i] = ma ...
- UESTC_酱神寻宝 2015 UESTC Training for Dynamic Programming<Problem O>
O - 酱神寻宝 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit ...
- 【LeetCode练习题】Combination Sum
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- unmount的时候报错
卸载存储的时候报错 device is busy 解决办法 例:/mnt/test 为存储挂载点 fuser -m -v /mnt/test fuser 可以显示出当前哪个程序在使用磁盘上的某个文件. ...
- 【转】android 电池(二):android关机充电流程、充电画面显示
关键词:android 电池关机充电 androidboot.mode charger关机充电 充电画面显示 平台信息:内核:linux2.6/linux3.0系统:android/android4. ...
- 如何煉成NET架構師
微软的DotNet 开发绝对是属于那种入门容易提高难的技术.而要能够成为DotNet 架构师没有三年或更长时间的编码积累基本上是不可能的.特别是在大型软件项目中,架构师是项目核心成员,承上启下,因此 ...
- c# 图片简单模糊 非高斯模糊
/// <summary> /// 图像模糊化 /// </summary> /// <param name="bit ...
- android 更改USB显示名称
能力 kernel\drivers\usb\gadget\Android.c 在这个例子中,下列的变化 #define PRODUCT_STRING "Sergeycao" 版权声 ...
- C语言指针的初始化和赋值
1.指针的初始化 指针初始化时,"="的右操作数必须为内存中数据的地址,不能够是变量,也不能够直接用整型地址值(可是int*p=0;除外,该语句表示指针为空).此时,*p仅仅是表示 ...
- 使用react-native做一个简单的应用-03欢迎界面
Android和iOS的欢迎界面是不一样的,在iOS中有一个默认的欢迎界面,而Android则需要自己写.因此我就分开说一下这两个平台的欢迎界面的搭建.下面先看一下实现效果: Android: iOS ...