-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0314-BinaryTreeVerticalOrderTraversal.cs
59 lines (51 loc) · 1.85 KB
/
0314-BinaryTreeVerticalOrderTraversal.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//-----------------------------------------------------------------------------
// Runtime: 288ms
// Memory Usage: 31.1 MB
// Link: https://leetcode.com/submissions/detail/367606994/
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
namespace LeetCode
{
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class _0314_BinaryTreeVerticalOrderTraversal
{
public IList<IList<int>> VerticalOrder(TreeNode root)
{
var results = new List<IList<int>>();
if (root == null) return results;
var columnTable = new Dictionary<int, IList<int>>();
var queue = new Queue<(int col, TreeNode node)>();
queue.Enqueue((0, root));
int minColumn = 0, maxColumn = 0;
while (queue.Count > 0)
{
(int col, TreeNode node) = queue.Dequeue();
if (!columnTable.ContainsKey(col))
columnTable[col] = new List<int>();
columnTable[col].Add(node.val);
minColumn = Math.Min(minColumn, col);
maxColumn = Math.Max(maxColumn, col);
if (node.left != null)
queue.Enqueue((col - 1, node.left));
if (node.right != null)
queue.Enqueue((col + 1, node.right));
}
for (int i = minColumn; i < maxColumn + 1; ++i)
results.Add(columnTable[i]);
return results;
}
}
}