mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
53 lines
2.6 KiB
HTML
53 lines
2.6 KiB
HTML
<p>Given the <code>root</code> of a binary tree, calculate the <strong>vertical order traversal</strong> of the binary tree.</p>
|
|
|
|
<p>For each node at position <code>(row, col)</code>, its left and right children will be at positions <code>(row + 1, col - 1)</code> and <code>(row + 1, col + 1)</code> respectively. The root of the tree is at <code>(0, 0)</code>.</p>
|
|
|
|
<p>The <strong>vertical order traversal</strong> of a binary tree is a list of top-to-bottom orderings for each column index starting from the leftmost column and ending on the rightmost column. There may be multiple nodes in the same row and same column. In such a case, sort these nodes by their values.</p>
|
|
|
|
<p>Return <em>the <strong>vertical order traversal</strong> of the binary tree</em>.</p>
|
|
|
|
<p> </p>
|
|
<p><strong>Example 1:</strong></p>
|
|
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/29/vtree1.jpg" style="width: 431px; height: 304px;" />
|
|
<pre>
|
|
<strong>Input:</strong> root = [3,9,20,null,null,15,7]
|
|
<strong>Output:</strong> [[9],[3,15],[20],[7]]
|
|
<strong>Explanation:</strong>
|
|
Column -1: Only node 9 is in this column.
|
|
Column 0: Nodes 3 and 15 are in this column in that order from top to bottom.
|
|
Column 1: Only node 20 is in this column.
|
|
Column 2: Only node 7 is in this column.</pre>
|
|
|
|
<p><strong>Example 2:</strong></p>
|
|
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/29/vtree2.jpg" style="width: 512px; height: 304px;" />
|
|
<pre>
|
|
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
|
|
<strong>Output:</strong> [[4],[2],[1,5,6],[3],[7]]
|
|
<strong>Explanation:</strong>
|
|
Column -2: Only node 4 is in this column.
|
|
Column -1: Only node 2 is in this column.
|
|
Column 0: Nodes 1, 5, and 6 are in this column.
|
|
1 is at the top, so it comes first.
|
|
5 and 6 are at the same position (2, 0), so we order them by their value, 5 before 6.
|
|
Column 1: Only node 3 is in this column.
|
|
Column 2: Only node 7 is in this column.
|
|
</pre>
|
|
|
|
<p><strong>Example 3:</strong></p>
|
|
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/29/vtree3.jpg" style="width: 512px; height: 304px;" />
|
|
<pre>
|
|
<strong>Input:</strong> root = [1,2,3,4,6,5,7]
|
|
<strong>Output:</strong> [[4],[2],[1,5,6],[3],[7]]
|
|
<strong>Explanation:</strong>
|
|
This case is the exact same as example 2, but with nodes 5 and 6 swapped.
|
|
Note that the solution remains the same since 5 and 6 are in the same location and should be ordered by their values.
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li>
|
|
<li><code>0 <= Node.val <= 1000</code></li>
|
|
</ul>
|