【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 ...
随机推荐
- FMXUI TEXTVIEW代码设置IMAGEINDEX
FMXUI作为一个开源的控件,真是DELPHIER的福音,向作者致敬.TEXTVIEW非常好用,在属性面板中有ImageIndex属性,可以方便设置图标,在实际应用中图标状态需要改变,但在代码设置时 ...
- Codeforces 803F - Coprime Subsequences(数论)
原题链接:http://codeforces.com/contest/803/problem/F 题意:若gcd(a1, a2, a3,...,an)=1则认为这n个数是互质的.求集合a中,元素互质的 ...
- 第二章--k-近邻算法(kNN)
一.k-近邻算法(kNN) 采用测量不同特征值之间的距离方法进行分类 工作原理: 存在一个样本数据集合(训练样本集),并且样本集中每个数据都存在标签,即我们知道样本集中每一数据与所属分类的对应关系.输 ...
- HihoCoder - 1673 (单调队列)
题目:https://vjudge.net/contest/319166#problem/A 题意:有一个01矩阵,求一个最大子矩阵面积,这个矩阵要求里面都是01间隔,没有0或1连续 思路:这个题其实 ...
- [codeforces 508E]Maximum Matching
题目:Maximum Matching 传送门:http://codeforces.com/contest/1038/problem/E 分析: 一个块拥有{color1,val,color2},两个 ...
- 5 August
P1016 旅行家的预算 单调队列. 再看看单调队列怎么用的. #include <cstdio> int n, l, r; double D, dd, d[9], C, p[9], an ...
- 团队冲刺DAY1
团队冲刺DAY1 今天的内容是对未来6天的突击有一个大致的规划. 我们小组的选题是客户端-服务器安全信息传递系统,通过讨论,我们认为大概有四个难题. 第一个是服务器和客户端,我们打算用第二天来完成. ...
- 题解 P1017 【进制转换】
我赶jio这个题难道是让我们写快写? 不管了,赶紧把咕咕咕了一万年的题解写出来. 这个题就是考察负进制和在mod意义下的除法运算的基础运算. (其实也没多大问题) 首先我们先假设一个原始数据\(num ...
- 测开之路五十六:实现类似unittest的断言
import inspect class Case(object): """ 实现断言 """ def __init__(self): se ...
- 第十三周学习总结&实验报告(八)
图像界面 件处理及监听处理 1.键盘事件(KeyEvent)及监听处理(KeyListener) 1.1加入事件监听 super.addWindowListener(new WindowAdapter ...