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,null,15,7],

  /\
/ \ /\
/ \

return its vertical order traversal as:

[
[],
[,],
[],
[]
]

Given binary tree [3,9,8,4,0,1,7],

    /\
/ \ /\ /\
/ \/ \

return its vertical order traversal as:

[
[],
[],
[,,],
[],
[]
]

Given binary tree [3,9,8,4,0,1,7,null,null,null,2,5] (0's right child is 2 and 1's left child is 5),

    /\
/ \ /\ /\
/ \/ \ /\
/ \

return its vertical order traversal as:

[
[],
[,],
[,,],
[,],
[]
]

思路:bsf。然后用链表暂时存储结果。假设当前链表节点的iterator为it,则它的前一个节点的iterator可以用std::prev(it, 1)获得,它的下一个节点的iterator可以用std::next(it, 1)获得。

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> verticalOrder(TreeNode* root) {
vector<vector<int> > res;
if (root == NULL) return res;
list<vector<int> > columns;
queue<TreeNode*> nodes;
queue<list<vector<int> >::iterator> iterators;
columns.push_back(vector<int>(, root->val));
nodes.push(root);
iterators.push(columns.begin());
while (!nodes.empty()) {
TreeNode* cur = nodes.front(); nodes.pop();
auto it = iterators.front(); iterators.pop();
if (cur->left) {
if (it == columns.begin()) columns.push_front(vector<int>(, cur->left->val));
else std::prev(it, )->push_back(cur->left->val);
nodes.push(cur->left);
iterators.push(std::prev(it, ));
}
if (cur->right) {
if (std::next(it, ) == columns.end()) columns.push_back(vector<int>(, cur->right->val));
else std::next(it, )->push_back(cur->right->val);
nodes.push(cur->right);
iterators.push(std::next(it, ));
}
}
for (auto it = columns.begin(); it != columns.end(); it++)
res.push_back(*it);
return res;
}
};

Binary Tree Vertical Order Traversal -- LeetCode的更多相关文章

  1. [Locked] Binary Tree Vertical Order Traversal

    Binary Tree Vertical Order Traversal Given a binary tree, return the vertical order traversal of its ...

  2. [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  3. LeetCode Binary Tree Vertical Order Traversal

    原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, ...

  4. LeetCode 314. Binary Tree Vertical Order Traversal

    原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, ...

  5. [LeetCode] 314. Binary Tree Vertical Order Traversal 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  6. [LeetCode] 314. Binary Tree Vertical Order Traversal 二叉树的垂直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  7. [leetcode]314. Binary Tree Vertical Order Traversal二叉树垂直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  8. Binary Tree Vertical Order Traversal

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  9. 314. Binary Tree Vertical Order Traversal

    题目: Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to ...

随机推荐

  1. android SharedPreferences 浅析

    1. 介绍:SharedPreferences 的作用是使用键值对的方式存储数据.且支持多种不同的数据类型存储: Android数据持久化方法中最简单的一种,即使用Preferences的键值对存储方 ...

  2. java初学2

    1.数组操作类Arrays与System public static void arraycopy(Object src, int srcPos, Object dest,int destPos,in ...

  3. ASP.NET Core [2]:Middleware-请求管道的构成(笔记)

    原文链接:http://www.cnblogs.com/RainingNight/p/middleware-in-asp-net-core.html 中间件处理请求主要分为三个阶段:1. 中间件的注册 ...

  4. 【志银】Dev-Cpp配置OpenGL图形库(成功版本:Dev-Cpp 5.7.1 MinGW 4.8.1)

    ★配置前须知:Dev-Cpp自带OpenGL的使用和OpenGL简介 (附Dev-Cpp下载地址:http://sourceforge.net/projects/orwelldevcpp/?sourc ...

  5. table不让td中文字溢出操作方法

    table不让td中文字溢出操作方法 table{ width:100px; table-layout:fixed;/* 只有定义了表格的布局算法为fixed,下面td的定义才能起作用. */ } t ...

  6. 【bzoj3144】[Hnoi2013]切糕 网络流最小割

    题目描述 输入 第一行是三个正整数P,Q,R,表示切糕的长P. 宽Q.高R.第二行有一个非负整数D,表示光滑性要求.接下来是R个P行Q列的矩阵,第z个 矩阵的第x行第y列是v(x,y,z) (1≤x≤ ...

  7. POJ 3977 Subset | 折半搜索

    题目: 给出一个整数集合,求出非空子集中元素和绝对值最小是多少(元素个数尽量少) 题解: 分成两半 爆搜每一半,用map维护前一半的值 每搜出后一半的一个值就去map里找和他和绝对值最小的更新答案 # ...

  8. AE中实现Control中的各种图形工具的方法

    添加命名空间 using ESRI.ArcGIS.SystemUI;using ESRI.ArcGIS.Controls; A类:前面有Controls 后面有tool的工具都可以用同一类的代码实现( ...

  9. 基于RRT的机器人自主探索建图

    一.方法讲解: 本项目分为三个部分:机器人周围一定范围内基于RRT的全局检测, 根据上一步检测的未知区域点执行sklearn.cluster.MeanShift聚类,获取聚类中心: 根据聚类中心计算各 ...

  10. mysql数据定期备份删除

    1.这里只写备份mysql的数据库的方法,全备份,包括定时删除. 准备工作,弄一个文件夹存数据库备份数据 第一步:编写shell 脚本,命名为shell.sh #备份数据库 backupdir=/we ...