Use of three sorted data structures in C#
Problem is this: https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree/description/
The solution will be an DFS (Depth-First-Search) with pre-order traversal. You'll keep track of the x-index and the y-index. So far, standard.
The catch is the use of the proper data structures to properly store this data in the right order. Here is what I decided to do:
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.
Example 1:
Input: [3,9,20,null,null,15,7] Output: [[9],[3,15],[20],[7]] Explanation: Without loss of generality, we can assume the root node is at position (0, 0): Then, the node with value 9 occurs at position (-1, -1); The nodes with values 3 and 15 occur at positions (0, 0) and (0, -2); The node with value 20 occurs at position (1, -1); The node with value 7 occurs at position (2, -2).
Example 2:
Input: [1,2,3,4,5,6,7] Output: [[4],[2],[1,5,6],[3],[7]] Explanation: The node with value 5 and the node with value 6 have the same position according to the given scheme. However, in the report "[1,5,6]", the node value of 5 comes first since 5 is smaller than 6.
Note:
- The tree will have between 1 and
1000
nodes. - Each node's value will be between
0
and1000
.
The catch is the use of the proper data structures to properly store this data in the right order. Here is what I decided to do:
- First, a Sorted Dictionary sorted by the x-index
- Second, the value of the above data structure will be a Sorted List sorted by the y-index
- Because of this statement, the value of the above data structure still needs to be sorted, hence I'm using as the value a Sorted Set to allow for duplicates
Putting it all together, it passes the tests. Cheers, ACC.
public class Solution { public IList<IList<int>> VerticalTraversal(TreeNode root) { SortedDictionary<int, SortedList<int, SortedSet<int>>> vertical = new SortedDictionary<int, SortedList<int, SortedSet<int>>>(); VerticalTraversal(root, 0, 0, vertical); List<IList<int>> solution = new List<IList<int>>(); foreach (int x in vertical.Keys) { List<int> list = new List<int>(); foreach (int y in vertical[x].Keys) foreach (int element in vertical[x][y]) list.Add(element); solution.Add(list); } return solution; } private void VerticalTraversal(TreeNode root, int x, int y, SortedDictionary<int, SortedList<int, SortedSet<int>>> vertical) { if (root == null) return; if (!vertical.ContainsKey(x)) vertical.Add(x, new SortedList<int, SortedSet<int>>()); if (!vertical[x].ContainsKey(y)) vertical[x].Add(y, new SortedSet<int>()); vertical[x][y].Add(root.val); VerticalTraversal(root.left, x - 1, y + 1, vertical); VerticalTraversal(root.right, x + 1, y + 1, vertical); } }
Nice, although Python 3 comes out a winner again in terms of performance with its 36ms :)
ReplyDeleteMy solution - https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree/discuss/232684/N*log(N)-solution-using-tree-traversal-and-dict-in-Python-(beats-100)
Nice!!!
ReplyDeleteThank you for sharing. I wrote a post to share based on your code. And also I made a comment about SortedSet to remove duplicate element, which may be a problem even though the online judge of Leetcode does not provide a test case to validate the case.
ReplyDeleteConcerns
SortedSet will remove duplicate elements, so it may be a problem to use it. Since there is no guarantee that the binary tree may have duplicate value, and those node with the same value may be in the same horizontal, same level of tree as well.
Here is the link:
https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree/discuss/304946/C-SortDictionary-SortedList-SortedSet-all-are-used-in-one-solution