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);
    }
}
相关推荐
ChillJavaGuy20 小时前
常见限流算法详解与对比
java·算法·限流算法
sali-tec20 小时前
C# 基于halcon的视觉工作流-章34-环状测量
开发语言·图像处理·算法·计算机视觉·c#
你怎么知道我是队长21 小时前
C语言---循环结构
c语言·开发语言·算法
艾醒21 小时前
大模型面试题剖析:RAG中的文本分割策略
人工智能·算法
纪元A梦1 天前
贪心算法应用:K-Means++初始化详解
算法·贪心算法·kmeans
_不会dp不改名_1 天前
leetcode_21 合并两个有序链表
算法·leetcode·链表
mark-puls1 天前
C语言打印爱心
c语言·开发语言·算法
Python技术极客1 天前
将 Python 应用打包成 exe 软件,仅需一行代码搞定!
算法
吃着火锅x唱着歌1 天前
LeetCode 3302.字典序最小的合法序列
leetcode
睡不醒的kun1 天前
leetcode算法刷题的第三十四天
数据结构·c++·算法·leetcode·职场和发展·贪心算法·动态规划