987. 二叉树的垂序遍历 - 力扣(LeetCode)

题目描述

给你二叉树的根结点 root ,请你设计算法计算二叉树的 垂序遍历 序列。

对位于 (row, col) 的每个结点而言,其左右子结点分别位于 (row + 1, col - 1) 和 (row + 1, col + 1) 。树的根结点位于 (0, 0) 。

二叉树的 垂序遍历 从最左边的列开始直到最右边的列结束,按列索引每一列上的所有结点,形成一个按出现位置从上到下排序的有序列表。如果同行同列上有多个结点,则按结点的值从小到大进行排序。

返回二叉树的 垂序遍历 序列。

题目示例

输入:root = [1,2,3,4,5,6,7]

输出:[[4],[2],[1,5,6],[3],[7]]

解释:

列 -2 :只有结点 4 在此列中。

列 -1 :只有结点 2 在此列中。

列 0 :结点 1 、5 和 6 都在此列中。

1 在上面,所以它出现在前面。

5 和 6 位置都是 (2, 0) ,所以按值从小到大排序,5 在 6 的前面。

列 1 :只有结点 3 在此列中。

列 2 :只有结点 7 在此列中。

解题思路

我们可以从根节点开始,对整棵树进行一次遍历,在遍历的过程中使用数组 nodes 记录下每个节点的行号 row,列号 col 以及值 value。在遍历完成后,我们按照 col 为第一关键字升序,row 为第二关键字升序,value 为第三关键字升序,对所有的节点进行排序即可。

在排序完成后,我们还需要按照题目要求,将同一列的所有节点放入同一个数组中。因此,我们可以对 nodes 进行一次遍历,并在遍历的过程中记录上一个节点的列号 lastcol。如果当前遍历到的节点的列号 col 与 lastcol 相等,则将该节点放入与上一个节点相同的数组中,否则放入不同的数组中。

参考代码

java 复制代码
class Solution {
    public List<List<Integer>> verticalTraversal(TreeNode root) {
        List<int[]> nodes = new ArrayList<>();
        dfs(root, 0, 0, nodes);
        Collections.sort(nodes, new Comparator<int[]>() {
            public int compare(int[] tuple1, int[] tuple2) {
                if(tuple1[0] != tuple2[0]) {
                    return tuple1[0] - tuple2[0];
                } else if(tuple1[1] != tuple2[1]) {
                    return tuple1[1] - tuple2[1];
                } else {
                    return tuple1[2] - tuple2[2];
                }
            }
        });
        List<List<Integer>> result = new ArrayList<>();
        int size = 0;
        int lastcol = Integer.MIN_VALUE;
        for(int[] tuple : nodes) {
            int col = tuple[0], row = tuple[1], value = tuple[2];
            if(col != lastcol) {
                lastcol = col;
                result.add(new ArrayList<Integer>());
                size++;
            }
            result.get(size - 1).add(value);
        }
        return result;
    }

    public void dfs(TreeNode node, int row, int col, List<int[]> nodes) {
        if(node == null) return;
        nodes.add(new int[]{col, row, node.val});
        dfs(node.left, row + 1, col - 1, nodes);
        dfs(node.right, row + 1, col + 1, nodes);
    }
}
相关推荐
lzh200409192 分钟前
【数据结构】二叉搜索树
数据结构·算法
c#上位机11 分钟前
halcon图像膨胀—dilation1
图像处理·算法·c#·halcon
RickyWasYoung12 分钟前
【聚类算法】高维数据的聚类
算法·数据挖掘·聚类
古月居GYH18 分钟前
数据结构算法——排序算法解析
数据结构·算法·排序算法
Zzzzmo_25 分钟前
【Java】排序算法(思路及图解)
算法·排序算法
人得思变~谁会嫌自己帅呢?28 分钟前
希尔排序算法
数据结构·算法·排序算法
福尔摩斯张33 分钟前
C语言文件操作详解(一):文件的打开与关闭(详细)
java·linux·运维·服务器·c语言·数据结构·算法
white-persist33 分钟前
【攻防世界】reverse | answer_to_everything 详细题解 WP
c语言·开发语言·汇编·python·算法·网络安全·everything
Ynchen. ~42 分钟前
[工程实战] 攻克“数据孤岛”:基于隐语纵向联邦学习的金融风控建模全解析
算法·金融·逻辑回归·隐语
程序员-King.1 小时前
day107—同向双指针—无重复字符的最长字串(LeetCode-3)
算法·leetcode·双指针