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. Springboot 启动问题

    每次以debug方式启动springboot之后都会在SilentExitExceptionHandler类中的throw new SilentExitException() 解决办法 :window ...

  2. 一个初学者的辛酸路程-依旧Django

    回顾: 1.Django的请求声明周期?   请求过来,先到URL,URL这里写了一大堆路由关系映射,如果匹配成功,执行对应的函数,或者执行类里面对应的方法,FBV和CBV,本质上返回的内容都是字符串 ...

  3. http协议--留

    1.http消息结构 *http客户端,即web浏览器,链接到服务器,向服务器发送一个http请求的目的 *http服务器,即web服务,接受请求,并向客户端发送http响应数据 http统一资源标识 ...

  4. [类和对象]4 C++ static & friend

    1.静态成员变量和成员函数 思考:每个变量,拥有属性.有没有一些属性,归所有对象拥有? 1.1 静态成员变量 1)定义静态成员变量 关键字 static 可以用于说明一个类的成员 静态成员提供了一个 ...

  5. 1079 Total Sales of Supply Chain (25 分)(树的遍历)

    给出一颗销售供应的树,树根唯一.在树根处货物的价格为p,然后从根节点开始没往结点走一层,该层的货物价格将会在父节点的价格上增加r%.给出每个叶节点的货物量求出他们的价格之和 #include<b ...

  6. 浅谈传输层协议TCP和UDP

    在当今因特网的层次结构中,传输层的协议主要有两种,其一为Transmission Control Protocol,即TCP:其二为User Datagram Protocol,即UDP. 1.TCP ...

  7. php设计模式 工厂模式和单例

    1.单例模式//让该类在外界无法造对象//让外界可以造一个对象,做一个静态方法返回对象//在类里面通过让静态变量控制返回对象只能是一个. class cat{ public $name; privat ...

  8. Beta

    目录 过去存在的问题 任务分工 规范 后端总结 卉卉 家灿 前端总结 绪佩 青元 恺琳 宇恒 丹丹 算法&API接口 家伟 鸿杰 一好 文档&博客撰写 政演 产品功能 我们已经坐了哪些 ...

  9. unity射线碰撞检测+LayerMask的使用

    射线在unity中是个很方便的东西,对对象查找.多用于碰撞检测(如:子弹飞行是否击中目标).角色移动等提供了很大的帮助,在此做个总结与大家分享下 ,若有不足欢迎吐槽 好了,话补多说啦,直接进入主题: ...

  10. RabbitMQ-Java客户端API指南-下

    RabbitMQ-Java客户端API指南-下 使用主机列表 可以将Address数组传递给newConnection().的地址是简单地在一个方便的类com.rabbitmq.client包与主机 ...