LeetCode 987. Vertical order traversal of binary tree (recursion / loop)

1. Title

Given a binary tree, traverse in descending order to return its node value.

For each node located at (X, Y), the left and right child nodes are located separately (X-1, Y-1) 和 (X+1, Y-1).

Move a vertical line from X = -infinity to X = + infinity, and whenever the vertical line touches the node, we report the value of the node in order from top to bottom (decreasing Y coordinate).

If two nodes the same location , the first to report the node the smaller value .

Returns the list of non-empty reports in order of X coordinate. Each report has a list of node values.

Example 1:
Insert picture description here

输入:[3,9,20,null,null,15,7]
输出:[[9],[3,15],[20],[7]]
解释: 
在不丧失其普遍性的情况下,我们可以假设根结点位于 (0, 0):
然后,值为 9 的结点出现在 (-1, -1);
值为 315 的两个结点分别出现在 (0, 0)(0, -2);
值为 20 的结点出现在 (1, -1);
值为 7 的结点出现在 (2, -2)

Example 2:
Insert picture description here

输入:[1,2,3,4,5,6,7]
输出:[[4],[2],[1,5,6],[3],[7]]
解释:
根据给定的方案,值为 56 的两个结点出现在同一位置。
然而,在报告 "[1,5,6]" 中,结点值 5 排在前面,因为 5 小于 6。
 
提示:
树的结点数介于 11000 之间。
每个结点值介于 01000 之间。

Source: LeetCode (LeetCode)
link: https://leetcode-cn.com/problems/vertical-order-traversal-of-a-binary-tree
copyright belongs to the deduction network. Please contact the official authorization for commercial reprint, and please indicate the source for non-commercial reprint.

2. Problem solving

  • map key record x coordinate, value record point collection{val, 深度}
  • Sort the same set of points as x, by depth first, value second

2.1 Recursion

class Solution {	
    map<int, vector<vector<int>>> m;//x坐标,节点集合< <val, deep> >
public:
    vector<vector<int>> verticalTraversal(TreeNode* root) {
        //前序遍历 根左右
        if(!root)
            return {};
        dfs(root,0,0);
        vector<vector<int>> temp;
        vector<vector<int>> ans(m.size());
        int i = 0, j;
        for(auto it  = m.begin(); it != m.end(); ++it)
        {
        	temp = it->second;//点集合
        	sort(temp.begin(), temp.end(),[&](auto a, auto b){
        		if(a[1] == b[1])
        			return a[0] < b[0];//深度一样,按值
        		return a[1] < b[1];//深度小的在前
        	});
            for(j = 0; j < temp.size(); ++j)
                ans[i].push_back(temp[j][0]);//数值写入答案
            i++;
        }
        return ans;
    }
    void dfs(TreeNode* root, int x, int deep)
    {
        if(!root)
            return;
        m[x].push_back({root->val,deep});
        dfs(root->left, x-1, deep+1);
        dfs(root->right, x+1, deep+1);
    }
};

24 ms 16.3 MB

2.2 Sequence traversal

class Solution {
public:
    vector<vector<int>> verticalTraversal(TreeNode* root) {
        map<int, vector<vector<int>>> m;//x坐标,节点集合< <val, deep> >
        if(!root)
            return {};
        queue<pair<TreeNode*,pair<int,int>>> q;//节点及其坐标x,y
        q.push({root,{0,0}});
        pair<TreeNode*,pair<int,int>> tp;
        TreeNode* node;
        int x, y;
        while(!q.empty())
        {
            tp = q.front();
            q.pop();
            node = tp.first;
            x = tp.second.first;
            y = tp.second.second;
            m[x].push_back(vector<int> {node->val,y});
            if(node->left)
                q.push({node->left, {x-1,y+1}});
            if(node->right)
                q.push({node->right, {x+1,y+1}});
        }
        vector<vector<int>> temp;
        vector<vector<int>> ans(m.size());
        int i = 0, j;
        for(auto it  = m.begin(); it != m.end(); ++it)
        {
        	temp = it->second;
        	sort(temp.begin(), temp.end(),[&](auto a, auto b){
        		if(a[1] == b[1])
        			return a[0] < b[0];//深度一样,按值
        		return a[1] < b[1];//深度小的在前
        	});
            for(j = 0; j < temp.size(); ++j)
                ans[i].push_back(temp[j][0]);//数值写入答案
            i++;
        }
        return ans;
    }
};

16 ms 13.2 MB

Published 907 original articles · Liked 2854 · Visit 490,000+

Guess you like

Origin blog.csdn.net/qq_21201267/article/details/105681731