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 to X = +infinity, whenever the vertical line touches some nodes, we report the values of the nodes in order from top to bottom (decreasing Y 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.

class Solution {
public:
vector<vector<int>> verticalTraversal(TreeNode* root) {
map<int,map<int,vector<int>>> mp;
vector<vector<int>> ret;
helper(root, , , mp);
for(auto it=mp.begin(); it!=mp.end(); it++){
vector<int> a;
for(auto iit=it->second.rbegin(); iit!=it->second.rend(); iit++) {
vector<int> tmp = iit->second;
sort(tmp.begin(), tmp.end());
for(auto x : tmp) a.push_back(x);
}
ret.push_back(a);
}
return ret;
}
void helper(TreeNode* root,int x,int y, map<int,map<int,vector<int>>>& mp){
if(!root) return;
mp[x][y].push_back(root->val);
helper(root->left, x-, y-, mp);
helper(root->right, x+, y-, mp);
}
};

LC 987. Vertical Order Traversal of a Binary Tree的更多相关文章

  1. LeetCode 987. Vertical Order Traversal of a Binary Tree

    原题链接在这里:https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree/ 题目: Given a binary ...

  2. 【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 ...

  3. 【LeetCode】987. Vertical Order Traversal of a Binary Tree 解题报告(C++ & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  4. [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 ...

  5. LeetCode Binary Tree Vertical Order Traversal

    原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, ...

  6. [Locked] Binary Tree Vertical Order Traversal

    Binary Tree Vertical Order Traversal Given a binary tree, return the vertical order traversal of its ...

  7. LeetCode 314. Binary Tree Vertical Order Traversal

    原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, ...

  8. Binary Tree Vertical Order Traversal -- LeetCode

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  9. [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 ...

随机推荐

  1. win10关闭防火墙和其通知

    Win10电脑在关闭防火墙后,防火墙的通知会不定期提醒,如果误点后,防火墙就悄悄的开启了,导致好多功能就用不了了,所以比较有效的方法是:关闭防火墙,并关闭防火墙通知 1.关闭防火墙 在控制面板中,选择 ...

  2. 目标检测 — one-stage检测(一)

    总结的很好:https://www.cnblogs.com/guoyaohua/p/8994246.html 目前主流的目标检测算法主要是基于深度学习模型,其可以分成两大类:two-stage检测算法 ...

  3. 关于Python的第一行语句

    通常在脚本语言的第一行会看到#!/usr/bin/env python 与 #!/usr/bin/python其中之一,这两句话的目的都是指出你的python文件用什么可执行程序去运行它. #!/us ...

  4. 用js刷剑指offer(二叉树的镜像)

    题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...

  5. Ubuntu系统---安装English版本之后的一些工作

                                                                      Ubuntu系统---安装English版本之后的一些工作 安装完U ...

  6. 洛谷P5002 专心OI - 找祖先

    题目概括 题目描述 这个游戏会给出你一棵树,这棵树有\(N\)个节点,根结点是\(R\),系统会选中\(M\)个点\(P_1,P_2...P_M\). 要Imakf回答有多少组点对\((u_i,v_i ...

  7. Week08_day01 (Hive实现WordCount计数)

    Hive实现WordCount计数 在没学习Hive之前,我们学习MapReduce去实现WordCount计数的时候,就要去编写80多行的java代码,现在我们学习了Hive,我们只需要一行Sql语 ...

  8. 0003SpringBoot整合SpringDataJPA

    SpringBoot整合SpringDataJpa步骤如下: 1.添加data-jpa起步依赖(pom.xml) 2.添加数据库驱动坐标.添加Junit起步依赖(pom.xml) 3.添加数据库连接信 ...

  9. Spring MVC + freemarker实现半自动静态化

    这里对freemarker的代码进行了修改,效果:1,请求.do的URL时直接生成对应的.htm文件,并将请求转发到该htm文件2,自由控制某个页面是否需要静态化原理:对org.springframe ...

  10. JavaScript设置和获取cookie

    创建 //创建cookie function setCookie(name, value, expires, path, domain, secure) { var cookieText = enco ...