LeetCode Binary Tree Vertical Order Traversal
原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/
题目:
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bottom, column by column).
If two nodes are in the same row and column, the order should be from left to right.
Examples:
Given binary tree [3,9,20,null,null,15,7]
,
3
/ \
9 20
/ \
15 7
return its vertical order traversal as:
[
[9],
[3,15],
[20],
[7]
]
Given binary tree [3,9,20,4,5,2,7]
,
_3_
/ \
9 20
/ \ / \
4 5 2 7
return its vertical order traversal as:
[
[4],
[9],
[3,5,2],
[20],
[7]
]
题解:
与Binary Tree Level Order Traversal类似。
BFS, 把TreeNode和它所在的col分别放到两个queue中. dequeue后放TreeNode到对应的 colunm bucket里.
Example of [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
. Notice that every child access changes one column bucket id. So 12
actually goes ahead of 11
.
Time Complexity: O(n). Space: O(n).
AC Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> verticalOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if(root == null){
return res;
}
HashMap<Integer, List<Integer>> colBucket = new HashMap<Integer, List<Integer>>();
LinkedList<TreeNode> que = new LinkedList<TreeNode>();
LinkedList<Integer> cols = new LinkedList<Integer>();
int min = 0;
int max = 0; que.add(root);
cols.add(0);
while(!que.isEmpty()){
TreeNode tn = que.poll();
int col = cols.poll();
if(!colBucket.containsKey(col)){
colBucket.put(col, new ArrayList<Integer>());
}
colBucket.get(col).add(tn.val);
min = Math.min(min, col);
max = Math.max(max, col); if(tn.left != null){
que.add(tn.left);
cols.add(col-1);
}
if(tn.right != null){
que.add(tn.right);
cols.add(col+1);
}
}
for(int i = min; i<=max; i++){
res.add(colBucket.get(i));
}
return res;
}
}
便于理解,可以吧TreeNode和它对应的column number放在一个Pair里.
Time Complexity: O(n). Space: O(n).
AC Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> verticalOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if(root == null){
return res;
} HashMap<Integer, List<Integer>> hm = new HashMap<Integer, List<Integer>>();
LinkedList<Pair> que = new LinkedList<Pair>();
que.add(new Pair(root, 0));
int min = 0;
int max = 0;
while(!que.isEmpty()){
Pair cur = que.poll();
if(!hm.containsKey(cur.col)){
hm.put(cur.col, new ArrayList<Integer>());
}
hm.get(cur.col).add(cur.tn.val);
max = Math.max(max, cur.col);
min = Math.min(min, cur.col); if(cur.tn.left != null){
que.add(new Pair(cur.tn.left, cur.col-1));
}
if(cur.tn.right != null){
que.add(new Pair(cur.tn.right, cur.col+1));
}
}
for(int i = min; i<=max; i++){
res.add(hm.get(i));
}
return res;
} public class Pair{
TreeNode tn;
int col;
public Pair(TreeNode tn, int col){
this.tn = tn;
this.col = col;
}
}
}
LeetCode Binary Tree Vertical Order Traversal的更多相关文章
- [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] 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 ...
- [Locked] Binary Tree Vertical Order Traversal
Binary Tree Vertical Order Traversal Given a binary tree, return the vertical order traversal of its ...
- 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] 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 @ Python
原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题意: Given a binary tree, ...
- LeetCode: Binary Tree Level Order Traversal 解题报告
Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...
随机推荐
- PHP日期与时间
时间戳是自 1970 年 1 月 1 日(00:00:00 GMT)以来的秒数.它也被称为 Unix 时间戳(Unix Timestamp).Unix时间戳(Unix timestamp),或称Uni ...
- php总结二篇
PHP的网站主要攻击方式: 1.命令注入(Command Injection) 2.eval注入(Eval Injection) 3.客户端脚本攻击(Script Insertion) 4.跨网站脚本 ...
- 剪短的python数据结构和算法的书《Data Structures and Algorithms Using Python》
按书上练习完,就可以知道日常的用处啦 #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving wit ...
- elk平台搭建
很多时候我们需要对日志做一个集中式的处理,但是通常情况下这些日志都分布到n台机器上面,导致一个结果就是效率比较低,而ELK平台可以帮助我们解决这么一件事情: ELK下载:https://www.ela ...
- JAVE not work in linux
1, it will print out exception, but still can convert the audio 2, it works in windows not linux, ne ...
- BZOJ 1076 & 撞鸭递推
题意: 还是看原题题面好... 你正在玩你最喜欢的电子游戏,并且刚刚进入一个奖励关.在这个奖励关里,系统将依次随 机抛出k次宝物,每次你都可以选择吃或者不吃(必须在抛出下一个宝物之前做出选择,且现在决 ...
- web项目绝对路径与相对路径的问题
1.绝对路径:就是一个文件url的全部或者磁盘完整的物理地址;例如 http://localhost:8080/navigation/a.jsp就是a.jsp的绝对路径,再例如 D:\TC\a.jsp ...
- 洛谷 P1373 小a和uim之大逃离 Label:dp 不会
题目背景 小a和uim来到雨林中探险.突然一阵北风吹来,一片乌云从北部天边急涌过来,还伴着一道道闪电,一阵阵雷声.刹那间,狂风大作,乌云布满了天空,紧接着豆大的雨点从天空中打落下来,只见前方出现了一个 ...
- iOS开发中常见问题集锦
在iOS开发中,会出现各种各样的问题.今天,就把这些常见的问题以及各位大牛的解决方案汇总下,方便以后查阅: 常见错误: 1. linker command failed with exit code ...
- JS正则表达式将url转成json格式
var url = location.search.substr(1); param = {}; console.log(url); url.replace(/([^?&]+)=([^?&am ...