[leetcode]314. 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 1:
Input:[3,9,20,null,null,15,7]
3
/\
/ \
9 20
/\
/ \
15 7 Output: [
[9],
[3,15],
[20],
[7]
]
思路
代码
/**
* 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) {
// corner
List<List<Integer>> results = new ArrayList<>();
if (root == null) return results;
// init
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
Map<Integer, List<Integer>> map = new HashMap<>();
Queue<Position> queue = new LinkedList<>(); //use queue to bfs
queue.add(new Position(root, 0));
while (!queue.isEmpty()) {
Position position = queue.remove();
min = Math.min(min, position.column);
max = Math.max(max, position.column);
List<Integer> list = map.get(position.column);
if (list == null) {
list = new ArrayList<>();
map.put(position.column, list);
}
list.add(position.node.val);
if (position.node.left != null) queue.add(new Position(position.node.left, position.column-1));
if (position.node.right != null) queue.add(new Position(position.node.right, position.column+1));
}
for(int i = min; i<= max; i++) {
List<Integer> list = map.get(i);
if (list != null) results.add(list);
}
return results;
}
} class Position {
TreeNode node;
int column;
Position(TreeNode node, int column) {
this.node = node;
this.column = column;
}
}
[leetcode]314. Binary Tree Vertical Order Traversal二叉树垂直遍历的更多相关文章
- [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 ...
- [LeetCode] 102. Binary Tree Level Order Traversal 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- LeetCode 314. Binary Tree Vertical Order Traversal
原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, ...
- [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 Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- 314. Binary Tree Vertical Order Traversal
题目: Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to ...
- [LC] 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 (二叉树的层序遍历)
题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...
随机推荐
- JpGraph使用详解之中文乱码解决方法
在前面的JpGraph使用详解这篇文章,已经对JpGraph的使用方法作了详细的交代,前面说好的,接下来解决中文乱码. JpGraph为什么会出现中文乱码 在JpGraph中默认是要把字符串转成utf ...
- Unreal Engine 4 反射机制的实现
转自:http://blog.csdn.net/noahzuo/article/details/51482255 反射机制 反射机制指的是程序可以在运行期间进行检测和修改自己状态的能力. UE4引擎使 ...
- python protobuf序列化repeated运用
下面是proto描述文件的定义 message Person { required string name = 1; required int32 id = 2; optional string em ...
- javascript继承之组合继承(三)
function Father(name) { this.name = name; } Father.prototype.say = function () { return this.name; } ...
- C# CPU,硬盘,mac地址灯本地信息查询
public class Computer { public static string CpuID; //1.cpu序列号 public static string MacAddress; //2. ...
- JavaScript中的坑
内容:关于JavaScript中的一些蛋疼的问题以及面试笔试中常见的一些坑爹套路总结 此部分内容持续总结完善中... 1.undefined和null的区别 null: Null类型,代表空值,代表一 ...
- Java内存原型分析:基本知识
转载: Java内存原型分析:基本知识 java虚拟机内存原型 寄存器:我们在程序中无法控制 栈:存放基本类型的数据和对象的引用,但对象本身不存放在栈中,而是存放在堆中 堆:存放用new产生的数据 静 ...
- CUDA C Programming Guide 在线教程学习笔记 Part 10【坑】
▶ 动态并行. ● 动态并行直接从 GPU 上创建工作,可以减少主机和设备间数据传输,在设备线程中调整配置.有数据依赖的并行工作可以在内核运行时生成,并利用 GPU 的硬件调度和负载均衡.动态并行要求 ...
- leetcode976
public class Solution { public int LargestPerimeter(int[] A) { var list = A.OrderByDescending(x => ...
- leetcode179
class Solution { public: string largestNumber(vector<int>& nums) { int n=nums.size(); vect ...