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. MVC方式显示数据(数据库)

    新建实体数据模型 选择ADO.NET实体数据模型,名称改为数据库名 因为使用现有数据库,所以选择来自数据库的EF设计器,只演示所以只选择一个表,空模型可后期增加表 选择从数据库更新模型 新建数据库连接 ...

  2. linux使用glibc版本安装mysql8.0.12

    1.前言 使用yum安装虽然很方便,但是如果要是在没有公网的环境下,是没有办法使用yum源的.所以我们可以使用mysql提供的glibc版本的安装包,进行安装. 但是在安装之前,一定要将以前的版本删除 ...

  3. 【Day5】3.反爬策略之模拟登录

    import urllib.request as ur import user_agent import lxml.etree as le request = ur.Request( url='htt ...

  4. python读txt数据报编码错误

    读数据代码: with open(path,'r') as f: for line in f: line = line.strip() 报错: UnicodeDecodeError: 'gbk' co ...

  5. RHEL7启动到命令模式

    打开/etc/inittab 文件会看到以下信息 从中知道想要启动后就进入完整的多用户文本模式(命令行模式) 以root权限执行: ln -sf /lib/systemd/system/multi-u ...

  6. PAT Basic 1055 集体照 (25 分)

    拍集体照时队形很重要,这里对给定的 N 个人 K 排的队形设计排队规则如下: 每排人数为 /(向下取整),多出来的人全部站在最后一排: 后排所有人的个子都不比前排任何人矮: 每排中最高者站中间(中间位 ...

  7. 【ecfinal2019热身赛】B题

    原题: 给你一个长度为1e5的序列ai,问你它的所有子序列的最大值与最小值之差的1000次方的和是多少 即∑_{p是a的子序列}(max{p}-min{p})^1000 这题难点在于(max-min) ...

  8. 关键字final 修饰类、方法、属性、参数类型

    笔记: /** 关键字final(最终的) 标记的类不能被继承, 提高安全性,提高程序的可读性 * 1.final 修饰类,这个类就不能被继承: 如:String类.StringBuffer类.Sys ...

  9. Mongo Restore

    #!/bin/sh HOST_IP=`/sbin/ifconfig | sed -n 's/.*inet addr:\([0-9.]\+\)\s.*/\1/p' | head -n1` echo &q ...

  10. 执行mysql脚本文件

    一般都是连接mysql执行sql语句: 在命令行下输入 mysql -h localhost -u root -p回车,然后输入密码即可; 或直接运行mysql自带的连接工具,然后输入密码即可. 执行 ...