【leetcode】987. Vertical Order Traversal of a Binary Tree
题目如下:
Given a binary tree, return the vertical order traversal of its nodes values.
For each node at position
(X, Y)
, its left and right children respectively will be at positions(X-1, Y-1)
and(X+1, Y-1)
.Running a vertical line from
X = -infinity
toX = +infinity
, whenever the vertical line touches some nodes, we report the values of the nodes in order from top to bottom (decreasingY
coordinates).If two nodes have the same position, then the value of the node that is reported first is the value that is smaller.
Return an list of non-empty reports in order of
X
coordinate. Every report will have a list of values of nodes.Example 1:
Input: [3,9,20,null,null,15,7]
Output: [[9],[3,15],[20],[7]]
Explanation:
Without loss of generality, we can assume the root node is at position (0, 0):
Then, the node with value 9 occurs at position (-1, -1);
The nodes with values 3 and 15 occur at positions (0, 0) and (0, -2);
The node with value 20 occurs at position (1, -1);
The node with value 7 occurs at position (2, -2).Example 2:
Input: [1,2,3,4,5,6,7]
Output: [[4],[2],[1,5,6],[3],[7]]
Explanation:
The node with value 5 and the node with value 6 have the same position according to the given scheme.
However, in the report "[1,5,6]", the node value of 5 comes first since 5 is smaller than 6.Note:
- The tree will have between 1 and
1000
nodes.- Each node's value will be between
0
and1000
.
解题思路:我的方法比较简单粗暴。用dic[x] = [(v,y)] 来记录树中每一个节点,x是节点的横坐标,y是纵坐标,v是值。接下来遍历树,并把每个节点都存入dic中。dic中每个key都对应Output中的一个list,按key值大小依次append到Output中,接下来再对每个key所对应的val按(v,y)优先级顺序排序即可。
代码如下:
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def verticalTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
dic = {}
queue = [(root,0,0)] #(node,x)
while len(queue) > 0:
node,x,y = queue.pop(0)
if x in dic:
dic[x].append((node.val,y)) # node.val,y
else:
dic[x] = [(node.val,y)]
if node.left != None:
queue.append((node.left,x-1,y-1))
if node.right != None:
queue.append((node.right,x+1,y-1)) res = []
keylist = sorted(dic.viewkeys()) def cmpf(v1,v2):
if v1[1] != v2[1]:
return v2[1] - v1[1]
return v1[0] - v2[0]
for i in keylist:
dic[i].sort(cmp=cmpf)
tl = []
for j in dic[i]:
tl.append(j[0])
res.append(tl)
return res
【leetcode】987. Vertical Order Traversal of a Binary Tree的更多相关文章
- 【LeetCode】987. Vertical Order Traversal of a Binary Tree 解题报告(C++ & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- LeetCode 987. Vertical Order Traversal of a Binary Tree
原题链接在这里:https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree/ 题目: Given a binary ...
- LC 987. Vertical Order Traversal of a Binary Tree
Given a binary tree, return the vertical order traversal of its nodes values. For each node at posit ...
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)
[LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [Swift]LeetCode987. 二叉树的垂序遍历 | Vertical Order Traversal of a Binary Tree
Given a binary tree, return the vertical order traversal of its nodes values. For each node at posit ...
- 【LeetCode】1161. Maximum Level Sum of a Binary Tree 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
- 【leetcode】331. Verify Preorder Serialization of a Binary Tree
题目如下: One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null ...
随机推荐
- CPU性能优化
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11521331.html CPU性能指标 根据指标找工具 根据工具查指标 top.vmstat 和 pi ...
- python学习笔记(三)字典操作和元组操作
字典: 字典也是我们开发过程中最常用的一种数据类型:具有极快的查找速度:字典是一种key-value的数据类型,比如说要存每个人的信息,那么每个人的编号就是key,value就是每个人的信息,这样的话 ...
- POJ 3130 How I Mathematician Wonder What You Are! (半平面相交)
Description After counting so many stars in the sky in his childhood, Isaac, now an astronomer and a ...
- keil c51 不能使用:Go to Definition of....的解决方法 STC51
keil c51 不能使用:Go to Definition of....的解决方法 达到的目标如下图所示: 解决方法为 :在工程栏右键单击进入Manage Components ,然后点确定,前提是 ...
- Android中实现Activity的启动拦截之----实现360卫士的安装应用界面
第一.摘要 今天不是周末,但是我已经放假了,所以就开始我们的技术探索之旅,今天我们来讲一下Android中最期待的技术,就是拦截Activity的启动,其实我在去年的时候,就像实现这个技术了,但是因为 ...
- 10.14 socket 网络编程
简单的例子 socket客户端 import socket client = socket.socket() #声明socket类型,同时生成socket连接对象 client.connect(('l ...
- CocoaPods中pod search报错的解决办法
pod search报错的原因: 每次使用pod search命令的使用会在该目录下生成一个search_index.json文件,会逐渐的增大,文件达到一定大小后,ruby解析里面的json字符就会 ...
- [CSP-S模拟测试76]题解
咕咕咕 A.序列 无解情况:$n>a*b$或$n<a+b-1$ 把序列分成B段,每段内部上升,各段分界处构成下降子序列. 实现并不是太简单,要动态地考虑一下边界什么的. #include& ...
- 后端技术杂谈4:Elasticsearch与solr入门实践
阮一峰:全文搜索引擎 Elasticsearch 入门教程 作者:阮一峰 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://g ...
- 嵌入式开发环境搭建(一) 虚拟机实现桥接Ethernet网口 并且通过WIFI进行NAT联网
背景: 目前手头上有一块JZ2440的板子,之前有搭建完整套开发环境,由于虚拟机故障需要从新搭建服务器端,故在此记录搭建步骤 环境: Ubuntu16.4 VMWare 12 先行条件: 先按照自定义 ...