[leetcode] Vertical Order Traversal of a Binary Tree

1. Traverse the binary tree, store {x,y,val} for every node in records;

2. Sort the records of {x,y,val} for all nodes by increasing x, decreasing y, and increasing val order;

3. Traverse the sorted records, for elements that have same x, put their vals in a vector one by one then push the vector in the answer to return;

struct x_y_val {
    int x;
    int y;
    int val;
    x_y_val(int _x, int _y, int _val):x(_x),y(_y),val(_val) {}
};

bool cmp (x_y_val a, x_y_val b) {
    if (a.x<b.x)
        return true;
    if (a.x==b.x&&a.y>b.y)
        return true;
    if (a.x==b.x&&a.y==b.y&&a.val<b.val)
        return true;
    return false;
}
class Solution {
public:
    //need to consider y, not only x.
    void mark_x_y(TreeNode* root, int x, int y, vector<x_y_val>& records) {
        if (root==NULL)
            return;
        records.push_back(x_y_val(x,y, root->val));
        mark_x_y(root->left, x-1, y-1, records);
        mark_x_y(root->right, x+1, y-1, records);
    }
    vector<vector<int>> verticalTraversal(TreeNode* root) {
        vector<vector<int>> answer;
        if (root==NULL)
            return answer;
        vector<x_y_val> records;
        mark_x_y(root, 0,0, records);
        sort(records.begin(), records.end(), cmp);
        vector<int> temp;
        int last_x=records[0].x;
        for (auto current:records) {
            if (current.x!=last_x) {
                answer.push_back(temp);
                temp.clear();
                last_x=current.x;
            }
            temp.push_back(current.val);
        }
        answer.push_back(temp);
        return answer;
    }
};

wow, i know it‘s poor though.

原文地址:https://www.cnblogs.com/RDaneelOlivaw/p/10366976.html

时间: 2024-11-09 04:57:03

[leetcode] Vertical Order Traversal of a Binary Tree的相关文章

[LeetCode 987] 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 =

【leetcode】987. 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 t

[Daily Coding Problem 223] O(1) space in order traversal of a binary tree

Typically, an implementation of in-order traversal of a binary tree has O(h) space complexity, where h is the height of the tree. Write a program to compute the in-order traversal of a binary tree using O(1) space. In-order traversal without recursio

[Locked] Binary Tree Vertical Order Traversal

Binary Tree Vertical Order Traversal Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bottom, column by column). If two nodes are in the same row and column, the order should be from left to right. Examp

LeetCode Binary Tree Vertical Order Traversal

原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bottom, column by column). If two nodes are in the same row and column, th

Leetcode 314. Binary Tree Vertical Order Traversal

Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bottom, column by column). If two nodes are in the same row and column, the order should be from left to right. Examples: Given binary tree [3,9,20,null,n

Binary Tree Vertical Order Traversal -- LeetCode

Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bottom, column by column). If two nodes are in the same row and column, the order should be from left to right. Examples: Given binary tree [3,9,20,null,n

*Binary Tree Vertical Order Traversal

Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bottom, column by column). If two nodes are in the same row and column, the order should be from left to right. Examples:Given binary tree [3,9,20,null,nu

314. Binary Tree Vertical Order Traversal

题目: Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bottom, column by column). If two nodes are in the same row and column, the order should be from left to right. Examples:Given binary tree [3,9,20,nul