【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 ...
随机推荐
- 51nod 1831: 小C的游戏(Bash博弈 找规律)
题目链接 此类博弈不需要考虑sg函数,只需要确定必胜态和必败态,解题思路一般为打败先打表找规律,而后找规律给出统一的公式.打表方式:给定初始条件(此题中为ok[0]=ok[1]=0),然后从低到高枚举 ...
- 0-2马尔可夫过程Markov Processes
在0-1中提到了,当最终output的p=0时,这个时候模型无法正常使用,为了解决这个问题,在0-4中会有所提及. 在本节中,其实,计算概率的时候,我们应该假设某一个位置的词与它前面的所有词都是相关的 ...
- Activity(工作流-1)
1.activity自带数据表的含义(23张表) (1)资源库流程规则表 1)act_re_deployment 部署信息表 2)act_re_model 流程设计模型部署表 3)act_re_pr ...
- shell脚本学习 (9) 提取开头或结尾的几行
1 提取开头的n行 用head awk或者 sed实现 do.txt sed 1q do.txt awk 'FNR <= 1' do.txt do.txt文件 2 显示行尾的几行 用tail - ...
- 4412 4路pwm输出
一.4412 xpwmTOUT1 这是4412的GPD0_1路,itop中被使用为LCD的背光电路的pwm功能.因此如果使用教程中的代码,同样操作GPD0_1是行不通的. 会出现错误,所以需要解除在内 ...
- ELK7.1.1之插件安装
在5.0版本之后不支持直接把插件包放入es安装目录的plugin目录下,需要单独安装:而且支持在线安装的插件很少,很多都是需要离线安装.以前的plugin变为elasticsearch-plugin ...
- 26 October in 614
Practice tower 有 \(N\,(2\le N\le 600000)\) 块砖,要搭一个 \(N\) 层的塔,要求:如果砖 \(A\) 在砖 \(B\) 上面,那么 \(A\) 不能比 \ ...
- wxparse使用(富文本插件)
优点:目前已知唯一可以转化HTML到小程序识别的插件 缺点:转换一个HTML标签可能需要大量的微信小程序标签还有样式 配置:第一步,下载 https://github.com/icindy/wxPar ...
- 三种数据库截取字段内容&&获取字符长度的函数如下
if(databaseutil.getValue("database").equalsIgnoreCase("sqlserver")){ list =categ ...
- Python Django 编写一个简易的后台管理工具1-安装环境
安装python环境 MAC 一般都会自带 Python2.x版本 的环境,你也可以在链接 https://www.python.org/downloads/mac-osx/ 上下载最新版安装. 安装 ...