987. Vertical Order Traversal of a Binary Tree Hard
1/**
2 * [987] Vertical Order Traversal of a Binary Tree
3 *
4 * Given the root of a binary tree, calculate the vertical order traversal of the binary tree.
5 * For each node at position (row, col), its left and right children will be at positions (row + 1, col - 1) and (row + 1, col + 1) respectively. The root of the tree is at (0, 0).
6 * The vertical order traversal 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.
7 * Return the vertical order traversal of the binary tree.
8 *
9 * Example 1:
10 * <img alt="" src="https://assets.leetcode.com/uploads/2021/01/29/vtree1.jpg" style="width: 431px; height: 304px;" />
11 * Input: root = [3,9,20,null,null,15,7]
12 * Output: [[9],[3,15],[20],[7]]
13 * Explanation:
14 * Column -1: Only node 9 is in this column.
15 * Column 0: Nodes 3 and 15 are in this column in that order from top to bottom.
16 * Column 1: Only node 20 is in this column.
17 * Column 2: Only node 7 is in this column.
18 * Example 2:
19 * <img alt="" src="https://assets.leetcode.com/uploads/2021/01/29/vtree2.jpg" style="width: 512px; height: 304px;" />
20 * Input: root = [1,2,3,4,5,6,7]
21 * Output: [[4],[2],[1,5,6],[3],[7]]
22 * Explanation:
23 * Column -2: Only node 4 is in this column.
24 * Column -1: Only node 2 is in this column.
25 * Column 0: Nodes 1, 5, and 6 are in this column.
26 * 1 is at the top, so it comes first.
27 * 5 and 6 are at the same position (2, 0), so we order them by their value, 5 before 6.
28 * Column 1: Only node 3 is in this column.
29 * Column 2: Only node 7 is in this column.
30 *
31 * Example 3:
32 * <img alt="" src="https://assets.leetcode.com/uploads/2021/01/29/vtree3.jpg" style="width: 512px; height: 304px;" />
33 * Input: root = [1,2,3,4,6,5,7]
34 * Output: [[4],[2],[1,5,6],[3],[7]]
35 * Explanation:
36 * This case is the exact same as example 2, but with nodes 5 and 6 swapped.
37 * Note that the solution remains the same since 5 and 6 are in the same location and should be ordered by their values.
38 *
39 *
40 * Constraints:
41 *
42 * The number of nodes in the tree is in the range [1, 1000].
43 * 0 <= Node.val <= 1000
44 *
45 */
46pub struct Solution {}
47use crate::util::tree::{TreeNode, to_tree};
48
49// problem: https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree/
50// discuss: https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree/discuss/?currentPage=1&orderBy=most_votes&query=
51
52// submission codes start here
53
54// Definition for a binary tree node.
55// #[derive(Debug, PartialEq, Eq)]
56// pub struct TreeNode {
57// pub val: i32,
58// pub left: Option<Rc<RefCell<TreeNode>>>,
59// pub right: Option<Rc<RefCell<TreeNode>>>,
60// }
61//
62// impl TreeNode {
63// #[inline]
64// pub fn new(val: i32) -> Self {
65// TreeNode {
66// val,
67// left: None,
68// right: None
69// }
70// }
71// }
72use std::rc::Rc;
73use std::cell::RefCell;
74impl Solution {
75 pub fn vertical_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<i32>> {
76 vec![]
77 }
78}
79
80// submission codes end
81
82#[cfg(test)]
83mod tests {
84 use super::*;
85
86 #[test]
87 fn test_987() {
88 }
89}
90
Back
© 2025 bowen.ge All Rights Reserved.