二叉树垂直遍历 · Binary Tree Vertical Order Traversal
[抄题]:
给定二叉树,返回其节点值的垂直遍历顺序。 (即逐列从上到下)。
如果两个节点在同一行和同一列中,则顺序应 从左到右。
给定一个二叉树 {3,9,20,#,#,15,7}
3
/\
/ \
9 20
/\
/ \
15 7
返回垂直遍历顺序:[[9],[3,15],[20],[7]]
给定一个二叉树 {3,9,20,#,#,15,7}
3
/\
/ \
9 8
/\ /\
/ \/ \
4 01 7
返回垂直遍历顺序:[[4],[9],[3,0,1],[8],[7]]
[暴力解法]:
时间分析:
空间分析:
[思维问题]:
- 完全不知道怎么找出数学关系,找出变化关系:列数从小到大,同一列时行数从小到大,从左到右
- 从左到右要用bfs来实现:添加一个点、扩展;添加一个点、扩展
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 不知道bfs怎么写,怎么扩展:树上进行的BFS是用左右节点来扩展的
- queue是用linkedlist实现的,不要死记硬背,直接联想图吧 用的是.poll()方法
- 已经创建出了qcol对象,判断它是否为空,用isempty(),没有对象才用null,要理解含义
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
树上进行的BFS是用左右节点来扩展的
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
- 每一列后面都是数字链表,用hashmap的value存储数字链表,和叶子顺序遍历相同
- 用两个队列:列数也是不断添加一个列、扩展到新涉及的列;添加一个列、扩展到新涉及的列
- hash.keySet()方法(方法也符合骆驼命名法)表示取出哈希表的所有key,Collections.min方法表示取出没有排序的哈希表的其最小值
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/ public class Solution {
/*
* @param root: the root of tree
* @return: the vertical order traversal
*/
public List<List<Integer>> verticalOrder(TreeNode root) {
//corner case
List<List<Integer>> ans = new ArrayList<>();
HashMap<Integer, List<Integer>> hash = new HashMap<>(); if (root == null) {
return ans;
}
Queue<Integer> qCol = new LinkedList<>();
Queue<TreeNode> qNode = new LinkedList<>();
//bfs
qCol.offer(0);
qNode.offer(root); while (!qCol.isEmpty()) {
int col = qCol.poll();
TreeNode node = qNode.poll(); hash.putIfAbsent(col,new ArrayList());
hash.get(col).add(node.val); if (node.left != null) {
qCol.offer(col - 1);
qNode.offer(root.left);
} if (node.right != null) {
qCol.offer(col + 1);
qNode.offer(root.right);
}
} for (int i = Collections.min(hash.keySet()); //
i <= Collections.max(hash.keySet()); i++) {
ans.add(hash.get(i));
} return ans;
}
}
二叉树垂直遍历 · Binary Tree Vertical Order Traversal的更多相关文章
- [Swift]LeetCode314. 二叉树的竖直遍历 $ Binary Tree Vertical Order Traversal
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- [Locked] Binary Tree Vertical Order Traversal
Binary Tree Vertical Order Traversal Given a binary tree, return the vertical order traversal of its ...
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- [LeetCode] 314. Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- LeetCode 102. 二叉树的层次遍历(Binary Tree Level Order Traversal) 8
102. 二叉树的层次遍历 102. Binary Tree Level Order Traversal 题目描述 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 每 ...
- LeetCode Binary Tree Vertical Order Traversal
原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, ...
- LeetCode 314. Binary Tree Vertical Order Traversal
原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, ...
- [LeetCode] 314. Binary Tree Vertical Order Traversal 二叉树的垂直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- [leetcode]314. Binary Tree Vertical Order Traversal二叉树垂直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
随机推荐
- Linux:自动获取静态IP地址,清空iptable,修改selinux脚本
自动获取静态IP地址,清空iptable,修改selinux脚本 环境:VMware 平台:centos6.8全新 功能: 1)应用ifconfig -a,route -n,cat /etc/reso ...
- ThinkTemplate模板引擎的设计和使用方法
在PHP开发的过程中,我们会接触到很多的模板引擎,包括FastTemplate.SmartTemplate.Smarty.tinybutstrong等,通常都是为了满足MVC开发模式的表现层需要,让显 ...
- HBase查询优化——持续更新
Scan:setBatch,setCaching,setCacheBlocks public void setBatch(int batch) public void setCaching(int c ...
- Spring IOC学习
IoC基础 控制反转IoC(Inversion of Control),是一种设计思想,DI(依赖注入)是实现IoC的一种方法,也有人认为DI只是IoC的另一种说法.没有IoC的程序中我们使用面向对象 ...
- 51Nod 1089:最长回文子串 V2(Manacher算法)
1089 最长回文子串 V2(Manacher算法) 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 回文串是指aba.abba.cccbccc.aaa ...
- 认识hammer.js
hammerjs是什么 hammerjs是一个短小精悍的库,他可以让我们轻松的实现移动端上的手势. hammerjs的两大优势如下: 为移动端网页添加相关手势. 去除移动端上的点击事件的300ms延迟 ...
- TypeScript学习笔记(五) - 泛型
本篇将介绍在TypeScript如何使用泛型. 一.泛型方法 在TypeScript里,声明泛型方法有以下两种方式: function generics_func1<T>(arg: T): ...
- 优先队列底层实现是堆(heap)(操作系统进程调度)
只有一个CPU的情况下,比如作业系统中的调度程序,当一个作业完成后,需要在所有等待调度的作业中选择一个优先级最高的作业来执行(删除),并且也可以添加一个新的作业到作业的优先队列中(插入). 插入操作 ...
- FFMPEG结构体分析:AVCodecContext(转)
注:写了一系列的结构体的分析的文章,在这里列一个列表: FFMPEG结构体分析:AVFrameFFMPEG结构体分析:AVFormatContextFFMPEG结构体分析:AVCodecContext ...
- bzoj 3144 [Hnoi2013]切糕——最小割
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3144 一根纵轴上切一个点,可以把一根纵轴上的点连成一串来体现.自己的写法是每个点连向前一个点 ...