二叉树垂直遍历 · 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 ...
随机推荐
- chrome 49 版本bug: flex父元素设置flex:1 , 子元素用height:100%无法充满父元素
1 <div class="container"> <div class="item"> <div class="ite ...
- 5.3 将users表添加到xadmin后台
在users模块中添加adminx.py文件,是xadmin后台管理默认的文件名,内容是: from .models import EmailVerifyRecord, Banner import x ...
- 基于tiny4412的u-boot移植(一)
作者信息 作者:彭东林 邮箱:pengdonglin137@163.com QQ: 405728433 平台介绍 开发环境:win7 64位 + VMware11 + Ubuntu14.04 64位 ...
- HDU4819 Mosaic【树套树】
LINK 题目大意 给你一个\(n*n\)矩阵,每个点有初始权值 q次询问每次把一个矩形的中心节点变成这个矩形中最大值和最小值的平均数 思路 很显然的树套树啊 就是一开始傻逼了没想到怎么去维护这个东西 ...
- UT源码+105032014070
设计三角形问题的程序 输入三个整数a.b.c,分别作为三角形的三条边,现通过程序判断由三条边构成的三角形的类型为等边三角形.等腰三角形.一般三角形(特殊的还有直角三角形),以及不构成三角形.(等腰直角 ...
- <script type="text/template">是干什么的,为什么要把html写在js中? 这是什么编程语言风格,都能这样用吗?
这一段存放了一个模板.在js里面,经常需要使用js往页面中插入html内容.比如这样: var number = 123; $('#d').append('<div class="t& ...
- 随笔:关于 FastAdmin ueditor 插件 中的 rand mt_rand mt_getrandmax 问题
随笔:关于 FastAdmin ueditor 插件 中的 rand mt_rand mt_getrandmax 问题 问题来源 一位小伙伴在使用 Ueditor 插件时出错,因为用的是 php7.1 ...
- IAR 9+ 编译 TI CC2541 出现 Segment ISTACK (size: 0xc0 align: 0) is too long for segment definition.
IAR 9+ 编译 TI CC2541 出现 Segment ISTACK (size: 0xc0 align: 0) is too long for segment definition. Segm ...
- Spring 部署Tomcat 404 错误解决方案
将Spring项目部署到tomcat后,访问网页出现404错误 HTTP Status 404 – Not Found The origin server did not find a current ...
- thinkphp3.2.3+smarty解决success调用模板错误心得
最近学习thinkphp上瘾,出现success找不到模板问题,查阅各大神解决方案,分享一下针对新手如何解决该问题,如有不对的地方请大神指正 1.首先修改自己的config文件,添加如下配置代码:// ...