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.

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()
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值