public class Solution
{
private Dictionary<int, List<KeyValuePair<int,int>>> dic = new Dictionary<int, List<KeyValuePair<int, int>>>();
private void SearchTree(TreeNode root,int val=,int depth=)
{
if(root != null)
{
if(dic.ContainsKey(val))
{
dic[val].Add(new KeyValuePair<int, int>(root.val,depth));
}
else
{
dic.Add(val, new List<KeyValuePair<int, int>>());
dic[val].Add(new KeyValuePair<int, int>(root.val, depth));
}
}
if(root.left!=null)
{
SearchTree(root.left, val - ,depth+);
}
if(root.right!=null)
{
SearchTree(root.right, val + ,depth+);
}
}
public IList<IList<int>> VerticalTraversal(TreeNode root)
{
var Li = new List<IList<int>>();
SearchTree(root);
var kvpairs = dic.OrderBy(x => x.Key).ToList();
foreach(var kv in kvpairs)
{
Li.Add(kv.Value.OrderBy(x => x.Value).ThenBy(x=>x.Key).Select(x=>x.Key).ToList());
}
return Li;
}
}

这道题的描述有一些不清楚,主要是If two nodes have the same position, then the value of the node that is reported first is the value that is smaller.

这一句,应该是先按照层排序,同层的节点再按照值从小到大排序。

如果没有主意到这个问题,会出现test case 13无法通过,截图如下:

因此,关键性的代码是下面这句,先按照层次排序,再按照值排序,再选择出值部分,用一个lambda解决。

Li.Add(kv.Value.OrderBy(x => x.Value).ThenBy(x=>x.Key).Select(x=>x.Key).ToList());

leetcode987的更多相关文章

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

随机推荐

  1. 在centos7中安装nodejs(npm )

    我当前使用的是Centos7 首先在官网查看当前最新的版本 https://nodejs.org/dist/ 我现在最新的是 https://nodejs.org/dist/latest-v10.x/ ...

  2. P1096(简单dp)

    题目描述 在N个数中找出其和为M的若干个数.先读入正整数N(1< N< 100)和M(1< M< 10000),  再读入N个正数(可以有相同的数字,每个数字均在1000以内) ...

  3. 学习笔记之Java

    Java (programming language) - Wikipedia https://en.wikipedia.org/wiki/Java_(programming_language) Ja ...

  4. Jmeter(十六)Logic Controllers 之 Runtime Controller

    Runtime Controller-----运行时间控制器:控制其下的Sampler运行时间. 该控制器较为简单,官方文档也没作太多说明.照着Blazemeter写个例子: 运行,查看结果. 可以看 ...

  5. .net core从依赖注入容器获取对象

    创建引擎方法:该方法用于在不使用构造注入的情况下从依赖注入容器中获取对象 /// <summary> /// 一个负责创建对象的引擎 /// </summary> public ...

  6. 2012 - AD FSMO操作主机角色 -- 作用

    林范围:(林中唯一) 架构主机角色(Schema Master) 定义所有域对象属性 域命名主机角色(Domain Naming Master) 控制域林内域的添加或删除(必须为GC)   域范围:( ...

  7. Linux安装jsvc,及Linux服务开发

    在linux上以服务的方式启动java程序,需要提前安装jsvc.linux是利用daemon(jsvc)构建java守护进程. 编译 daemon 安装JSVC 1 下载文件,http://comm ...

  8. AWS之搭建深度学习主机

    B.G 至2017.11 GPU选型:(参考知乎.CSDN) 高性价比的两款:GTX1080ti, Titan X --通常调试算法  i7CPU+32G内存+SSD+2*GPU(单块显存>6G ...

  9. raid1与raid5

    raid 1 就是两个磁盘同时读同时写, 当其中一个坏了 不影响使用, 直接更换一个,这样磁盘的容量只有一个盘的raid 5 就是 N-1个磁盘的容量,当其中任何一个磁盘坏,不影响使用,更换一个就可以 ...

  10. Python网络爬虫之requests模块

    今日内容 session处理cookie proxies参数设置请求代理ip 基于线程池的数据爬取 知识点回顾 xpath的解析流程 bs4的解析流程 常用xpath表达式 常用bs4解析方法 引入 ...