LeetCode "Binary Tree Vertical Order"

BFS + HashTable

class Solution
{
    int maxl, minl;
    unordered_map<int, vector<int>> hm;
public:
    vector<vector<int>> verticalOrder(TreeNode* root) {
        maxl = INT_MIN;
        minl = INT_MAX;

        typedef pair<TreeNode*, int> Rec;
        queue<Rec> q;
        if (root)
        {
            q.push(Rec(root, 0));
        }
        while (!q.empty())
        {
            Rec curr = q.front(); q.pop();
            int l = curr.second;
            maxl = max(maxl, l);
            minl = min(minl, l);

            TreeNode *tmp = curr.first;
            hm[l].push_back(tmp->val);

            if (tmp->left)
            {
                q.push(Rec(tmp->left, l - 1));
            }
            if (tmp->right)
            {
                q.push(Rec(tmp->right, l + 1));
            }
        }

        vector<vector<int>> ret;
        for (int i = minl; i <= maxl; i++)
        {
            if (hm[i].size() == 0) continue;
            ret.push_back(hm[i]);
        }

        return ret;
    }
};
时间: 2024-10-11 13:18:35

LeetCode "Binary Tree Vertical Order"的相关文章

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]Binary Tree Level Order Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal/ 题意:二叉树的层序遍历的实现. 解题思路:二叉树的层序遍历可以用bfs或者dfs来实现.这里使用的dfs实现,代码比较简洁.实际上,二叉树的先序遍历就是dfs实现.   比如一棵树如下: 1 /  \ 2       3 /    \    /   \ 4     5  6    7    二叉树的先序遍历为{1,2,4,5,3,6,7},可以看到这个遍

[leetcode]Binary Tree Level Order Traversal II @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题意: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary

[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 Level Order Traversal

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] 原题链接:

LeetCode——Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order trave

LeetCode: Binary Tree Level Order Traversal II [107]

[题目] Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order

[LintCode] 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. Have you met this question in a real inter

[LeetCode]Binary Tree Level Order Traversal II

Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 re