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. 关于dubbo通信协议之对比

    对dubbo的协议的学习,可以知道目前主流RPC通信大概是什么情况, dubbo共支持如下几种通信协议: dubbo:// rmi:// hessian:// http:// webservice:/ ...

  2. ALGO-27_蓝桥杯_算法训练_FBI树(树,递归)

    问题描述 我们可以把由“”和“”组成的字符串分为三类:全“”串称为B串,全“”串称为I串,既含“”又含“”的串则称为F串. FBI树是一种二叉树,它的结点类型也包括F结点,B结点和I结点三种.由一个长 ...

  3. STL基础--迭代器和算法

    1 迭代器 Iterators 5种迭代器类型 随机访问迭代器: vector, deque, array // 允许的操作 vector<int> itr; itr = itr + 5; ...

  4. live555源码学习笔记之TaskScheduler

    今天抽空研究了下live555的任务实现: TaskScheduler分为三种任务:socket handler,event handler,delay task.这三种任务的特点是,前两个加入执行队 ...

  5. C#、AE开发入门之打开TIFF文件并显示

    继上篇文章,本次打开TIFF文件,附上源码及其注释 private void button2_Click(object sender, EventArgs e) { axMapControl1.Cle ...

  6. 用sqoop将mysql的数据导入到hive表

    一.先将mysql一张表的数据用sqoop导入到hdfs 1.1.先在mysql中准备一张测试用的表 mysql> desc user_info; +-----------+---------- ...

  7. [转]SQL数据库查询到的汉字字段是乱码

    使用英文版SQL数据库查询到的汉字字段是乱码的解决方案 2007-12-04 14:55:45 标签:函数 SQL 数据库 乱码 排序规则 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出 ...

  8. hadoop本地运行模式调试

    一:简介 最近学习hadoop本地运行模式,在运行期间遇到一些问题,记录下来备用:以运行hadoop下wordcount为例子. hadoop程序是在集群运行还是在本地运行取决于下面两个参数的设置,第 ...

  9. qsort实现结构体数组排序

    要注意强制转换 #include <stdio.h> #include <stdlib.h> typedef struct{ int num; char name[20]; f ...

  10. Mybatis 系列2-配置文件

    [Mybatis 系列10-结合源码解析mybatis 执行流程] [Mybatis 系列9-强大的动态sql 语句] [Mybatis 系列8-结合源码解析select.resultMap的用法] ...