二叉树的层次遍历 · Binary Tree Level Order Traversal
[抄题]:
给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问)
[思维问题]:
[一句话思路]:
用queue存每一层
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 每一层level存的东西都是不一样的,所以要在queue非空的循环中再新建。不要当成全局变量。
- root为空时,返回result空数组,而不是null四个字母
- stack用pop 弹出来,queue用poll 拉出来,想想也比较形象。root非空用null,数据结构非空要用isEmpty()函数
[二刷]:
- 不要忘了写特判
[三刷]:
[四刷]:
[五刷]:
[总结]:
把size单独设成变量 ,每次都取一遍 多取几次总不会错 (否则取左、右会导致queue.size()很大,level会多加)
树的BFS不会重复,所以不需要用哈希表。只要用queue即可。
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
- 用queue存每一层,实现先进先出
- 注意格式:接口 名 = new 具体实现;eg 队列可以用静态数组、链表linked list来实现
- List<List<Integer>> result = new ArrayList<List<Integer>>(); list里面装list,效果是一层一层的:
[
[3],
[9,20],
[15,7]
]
[其他解法]:
2queue,dummy node
[Follow Up]:
- 把结果反过来写:用Collections.reverse(result);函数
- DFS:迭代式的深度优先搜索,不断放宽MAX_LEVEL条件,直到某层没有点。(历史上,内存有限的时候用)
[LC给出的题目变变变]:
637. Average of Levels in Binary Tree
103. Binary Tree Zigzag Level Order Traversal
public class Solution {
/*
* @param root: A Tree
* @return: Level order a list of lists of integer
*/
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
Queue<TreeNode> queue = new LinkedList<TreeNode>(); if (root == null) {
return result;
} queue.offer(root);
while (!queue.isEmpty()) {//
int size = queue.size();
List<Integer> level = new ArrayList<Integer>();
for (int i = 0; i < size; i++) {
TreeNode curt = queue.poll();//
level.add(curt.val);
if (curt.left != null) {
queue.offer(curt.left);
}
if (curt.right != null) {
queue.offer(curt.right);
}
}
result.add(level);
}
return result;
}
}
二叉树的层次遍历 · Binary Tree Level Order Traversal的更多相关文章
- LeetCode 102. 二叉树的层次遍历(Binary Tree Level Order Traversal) 8
102. 二叉树的层次遍历 102. Binary Tree Level Order Traversal 题目描述 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 每 ...
- [Swift]LeetCode102. 二叉树的层次遍历 | Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- (二叉树 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 ...
- 二叉树叶子顺序遍历 · binary tree leaves order traversal
[抄题]: 给定一个二叉树,像这样收集树节点:收集并移除所有叶子,重复,直到树为空. 给出一个二叉树: 1 / \ 2 3 / \ 4 5 返回 [[4, 5, 3], [2], [1]]. [暴力解 ...
- [LintCode] Binary Tree Level Order Traversal(二叉树的层次遍历)
描述 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 样例 给一棵二叉树 {3,9,20,#,#,15,7} : 3 / \ 9 20 / \ 15 7 返回他的分层遍历结果: [ [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 ...
- [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 层序遍历二叉树
Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its ...
随机推荐
- MYSQL中只知表名查询属于哪个SCHEMA
只知道表名XXX查该表属于哪个schema.以及该表有哪些列等信息 SELECT * from information_schema.columns WHERE table_name = 'xxx'; ...
- java设计模式—工厂模式
一.工厂模式主要是为创建对象提供过渡接口,以便将创建对象的具体过程屏蔽隔离起来,达到提高灵活性的目的. 工厂模式在<Java与模式>中分为三类:1)简单工厂模式(Simple Factor ...
- ubuntu 18.04下svn的安装与基本命令
一.安装: apt-get install subversion 二.基本命令 1.将文件checkout到本地目录 svn checkout path(path是服务器 上的目录)例如:svn ch ...
- 面对最菜TI战队,OpenAI在Dota2上输的毫无还手之力
作者:Tony Peng 去年,OpenAI 的 1v1 AI 击败了世界顶尖选手 Dendi,OpenAI CTO Greg Brockman 承诺:明年,我们会带着 5v5 的 AI bot 重回 ...
- python中__init__()、__new__()、__call__()、__del__()几个魔法方法的用法
关于__new__()的用法参考: http://www.myhack58.com/Article/68/2014/48183.htm 正文: 一.__new__()的用法: __new__()是在新 ...
- mysql 解除安全模式
问题:rror Code: 1175. You are using safe update mode and you tried to update a table without a WHERE t ...
- apache配置修改
1.如何设置请求等待时间 在httpd.conf里面设置: TimeOut n 其中n为整数,单位是秒. 2. 3.如何使得apache监听在特定的端口 修改httpd.con ...
- jquery 的 $.extend 和 $.fn.extend
$.extend({ add:function(a,b){return a+b;}, bad:function(a,b){return a-b;} }); $.fn.extend({ loading: ...
- 使用MATPLOTLIB 制图(散点图,热力图)
import numpy as np import pandas as pd import matplotlib.pyplot as plt data = pd.read_csv('D:\\myfil ...
- VBA 操作 VBE
Introduction You can write code in VBA that reads or modifies other VBA projects, modules, or proced ...