[LC] 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; }
* }
*/
class Solution {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
public List<List<Integer>> verticalOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) {
return res;
}
dfs(root, 0);
for (int i = min; i <= max; i++) {
res.add(new ArrayList<>());
} Queue<TreeNode> queue = new LinkedList<>();
Queue<Integer> indexQ = new LinkedList<>();
queue.offer(root);
// map the leftmost to 0, so that root is -min
indexQ.offer(-min);
while (!queue.isEmpty()) {
TreeNode cur = queue.poll();
int curIndex = indexQ.poll();
res.get(curIndex).add(cur.val);
if (cur.left != null) {
queue.offer(cur.left);
indexQ.offer(curIndex - 1);
}
if (cur.right != null) {
queue.offer(cur.right);
indexQ.offer(curIndex + 1);
}
}
return res;
} private void dfs(TreeNode root, int num) {
if (root == null) {
return;
}
min = Math.min(min, num);
max = Math.max(max, num);
dfs(root.left, num - 1);
dfs(root.right, num + 1);
} }
[LC] 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
原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, ...
- 314. Binary Tree Vertical Order Traversal
题目: Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to ...
- [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 ...
- [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 Binary Tree Vertical Order Traversal
原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, ...
- Binary Tree Vertical Order Traversal
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
随机推荐
- Nginx之epoll和select poll
epoll和 select poll 都是做I/O多路复用的. 区别在于: epoll较灵活,如果有一百万个链接状态同时保持,但是在某个时刻,只有几百个链接是活跃的.epoll的处理是通过epoll_ ...
- SQL字符替换函数translater, replace
translate() 函数原型是:translate(string, from, to) SELECT TRANSLATE('12345', '134', 'ax') 得到:a2x5 这个函数会把f ...
- Mac电脑如何彻底删除node
之前本来想搭建一个hexo来写博客的,但是最后还是放弃,老老实实就在博客园和CSDN写博文了,这里记录一下怎么在Mac电脑下彻底删除node.js的方法 下面这个方法是我结合了网上好几个方法综合在一起 ...
- php中const和define的区别
define部分:宏不仅可以用来代替常数值,还可以用来代替表达式,甚至是代码段.(宏的功能很强大,但也容易出错,所以其利弊大小颇有争议.)宏的语法为:#define 宏名称 宏值作为一种建议和一种广大 ...
- java AES加解密
AES加解密工具类 package com.yan.demo; import org.apache.commons.lang3.StringUtils; import sun.misc.BASE64D ...
- zabbix中文乱码解决
问题现象: zabbix字体修改成中文后监控显示乱码 原因: 该问题是由于zabbix默认使用的是“DejaVuSans.ttf”(zabbix3.2.7默认使用的是“graphfont.ttf”), ...
- MVC——EF 回顾总结
回顾一下MVC的知识点. 其实开始 我在学校的知识对MVC 还是很模糊的一个概念.只是记得结合EasyUI 增删改查 和分页,代码都是模糊的 进过这段时间的学习,让我对MVC 有了一个很清楚的认识. ...
- 108.生成和下载csv文件
生成CSV文件 有时候我们做的网站,需要将一些数据,生成一个csv文件返回浏览器,并且是作为附件的形式下载下来. 生成小的csv文件: 生成一个小的csv文件,我们用Python内置的csv模块来处理 ...
- delphpi tcp 服务和客户端 例子
//服务器端unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, ...
- php 随机useragent
<?php /** * 获取随机useragent */ private function get_rand_useragent($param) { $arr = array( 'Mozilla ...