• LeetCode每日一题(987. Vertical Order Traversal of a Binary Tree)


    Given the root of a binary tree, calculate the vertical order traversal of the binary tree.

    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).

    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.

    Return the vertical order traversal of the binary tree.

    Example 1:

    Input: root = [3,9,20,null,null,15,7]
    Output: [[9],[3,15],[20],[7]]

    Explanation:
    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.

    Example 2:

    Input: root = [1,2,3,4,5,6,7]
    Output: [[4],[2],[1,5,6],[3],[7]]
    Explanation:
    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.
    
    • 1
    • 2

    Column 1: Only node 3 is in this column.
    Column 2: Only node 7 is in this column.

    Example 3:

    Input: root = [1,2,3,4,6,5,7]
    Output: [[4],[2],[1,5,6],[3],[7]]

    Explanation:
    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.

    Constraints:

    • The number of nodes in the tree is in the range [1, 1000].
    • 0 <= Node.val <= 1000

    头一次做到难度向下越级的题,这题标着 hard,其实比很多 medium 的要简单。
    其实就是给每个节点赋坐标,根节点 row=0, col =0, 然后左侧子节点 row += 1, col -= 1, 右侧子节点 row += 1, col += 1, 将所有节点的坐标按 col 进行收集, 同时按 row, val 进行排序(因为是从上到下从左到右的顺序), 最后将 val 拿出来即可。
    代码实现中是按照 row, col, val 进行排序的, 但是因为坐标是收集在同一个 col 下面的, 所以 col 都是相同的, 也就是没有什么意义, 但是也懒得改了, 就这样吧


    
    use std::cell::RefCell;
    use std::collections::{BinaryHeap, HashMap};
    use std::rc::Rc;
    impl Solution {
        fn dfs(
            root: Option<Rc<RefCell<TreeNode>>>,
            row: i32,
            col: i32,
            columns: &mut HashMap<i32, BinaryHeap<(i32, i32, i32)>>,
        ) {
            if let Some(node) = root {
                columns
                    .entry(col)
                    .or_insert(BinaryHeap::new())
                    .push((row, col, node.borrow().val));
                Solution::dfs(node.borrow_mut().left.take(), row + 1, col - 1, columns);
                Solution::dfs(node.borrow_mut().right.take(), row + 1, col + 1, columns);
            }
        }
        pub fn vertical_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<i32>> {
            let mut columns = HashMap::new();
            Solution::dfs(root, 0, 0, &mut columns);
            let mut l: Vec<(i32, Vec<(i32, i32, i32)>)> = columns
                .into_iter()
                .map(|(k, v)| (k, v.into_sorted_vec()))
                .collect();
            l.sort_by_key(|(c, _)| *c);
            l.into_iter()
                .map(|(_, v)| v.into_iter().map(|(_, _, vv)| vv).collect())
                .collect()
        }
    }
    
    
    • 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
  • 相关阅读:
    TCP/IP_第八章_静态路由_实验案例一
    springboot thymeleaf使用
    Java单例模式
    YOLOv3目标检测全过程记录
    猿创征文|瑞吉外卖——移动端_购物车
    2.9 场景式文案,原来是这样子写的【玩赚小红书】
    某MDM主数据管理系统与微软Dynamic CRM系统(新加坡节点)集成案例
    解决:java: 错误: 不支持发行版本 5 最有效方法
    JDBC的基本使用(mysql与java)
    如何找到联盟营销人员:招募合适会员的10个方法
  • 原文地址:https://blog.csdn.net/wangjun861205/article/details/126686917