987. Vertical Order Traversal of a Binary Tree**

987. Vertical Order Traversal of a Binary Tree**

https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree/

题目描述

Given a binary tree, return the vertical order traversal of its nodes values.

For each node at position (X, Y), its left and right children respectively will be at positions (X-1, Y-1) and (X+1, Y-1).

Running a vertical line from X = -infinity to X = +infinity, whenever the vertical line touches some nodes, we report the values of the nodes in order from top to bottom (decreasing Y coordinates).

If two nodes have the same position, then the value of the node that is reported first is the value that is smaller.

Return an list of non-empty reports in order of X coordinate. Every report will have a list of values of nodes.

Example 1:

Input: [3,9,20,null,null,15,7]
Output: [[9],[3,15],[20],[7]]
Explanation: 
Without loss of generality, we can assume the root node is at position (0, 0):
Then, the node with value 9 occurs at position (-1, -1);
The nodes with values 3 and 15 occur at positions (0, 0) and (0, -2);
The node with value 20 occurs at position (1, -1);
The node with value 7 occurs at position (2, -2).

Example 2:

Input: [1,2,3,4,5,6,7]
Output: [[4],[2],[1,5,6],[3],[7]]
Explanation: 
The node with value 5 and the node with value 6 have the same position according to the given scheme.
However, in the report "[1,5,6]", the node value of 5 comes first since 5 is smaller than 6.

Note:

  • The tree will have between 1 and 1000 nodes.
  • Each node’s value will be between 0 and 1000.

C++ 实现 1

先要理解题意:

了解题意之后, 可以考虑用 tuple 来存储每个节点的坐标和对应的值 (x, y, val), tuple 中的内容可以通过前序遍历获取. 之后就要对 tuple 进行排序:

扫描二维码关注公众号,回复: 9724035 查看本文章
  • x 最小的排前面
  • x 相等但是 y 较大的排前面
  • xy 均相等, 但是值 val 最小的排前面

注意最后的输出结果中, 是按照 x 的值来分组的, 所以还需要简单的处理一下.

class Solution {
private:
    vector<tuple<int, int, int>> record;
    void preorder(TreeNode *root, int x, int y) {
        if (!root) return;
        record.push_back({x, y, root->val});
        preorder(root->left, x - 1, y - 1);
        preorder(root->right, x + 1, y - 1);
    }
public:
    vector<vector<int>> verticalTraversal(TreeNode* root) {
        if (!root) return {};
        preorder(root, 0, 0);
        std::sort(record.begin(), record.end(), 
            [] (const tuple<int, int, int> &p, const tuple<int, int, int> &q) {
                return std::get<0>(p) < std::get<0>(q) ||
                    (std::get<0>(p) == std::get<0>(q) &&
                    std::get<1>(p) > std::get<1>(q)) ||
                    (std::get<0>(p) == std::get<0>(q) &&
                    std::get<1>(p) == std::get<1>(q) &&
                    std::get<2>(p) < std::get<2>(q));
            }
        );
        vector<vector<int>> res;
        for (int i = 0; i < record.size(); ++ i) {
            int j = i;
            vector<int> col;
            // 由于 record 已经是排序好了的, 用 record[i, ..., j) 表示这段范围内
            // x 相等的结果
            while (j < record.size()) {
                if (std::get<0>(record[i]) != std::get<0>(record[j])) break;
                else col.push_back(std::get<2>(record[j++]));
            }
            i = --j;
            res.push_back(col);
        }
        return res;
    }
};
发布了394 篇原创文章 · 获赞 8 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Eric_1993/article/details/104690141