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,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的更多相关文章
- [Locked] Binary Tree Vertical Order Traversal
Binary Tree Vertical Order Traversal Given a binary tree, return the vertical order traversal of its ...
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- LeetCode Binary Tree Vertical Order Traversal
原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, ...
- LeetCode 314. Binary Tree Vertical Order Traversal
原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, ...
- [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 ...
- [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 ...
- [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 ...
- Binary Tree Vertical Order Traversal
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- 314. Binary Tree Vertical Order Traversal
题目: Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to ...
随机推荐
- NGUI-使用UILabel呈现图片和不同格式的文字
1.可以使用BBCode标记 [b]Bold[/b] 粗体[i]italic[/i] 斜体[u]underli ...
- python学习总结---函数使用 and 生成器
# 函数使用 ### 生成器 - 使用场景 在使用列表时,很多时候我们不会一下子使用全部数据,通常都是一个一个使用,但是当数据量比较大的时候,定义一个大的列表将会是内容使用突然增大.为了解决此类问题, ...
- CSU-1170 A Simple Problem
题目链接 http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=1170 题目 Description 在一个由N个整数组成的数列中,最 ...
- NodeJs01 文件浏览器
ES6常用新语法 前言 是时候学点新的JS了! 为了在学习NodeJs之前,能及时用上语言的新特性,我们打算从一开始先学习一下JavaScript语言的最基本最常用新语法.本课程的内容,是已经假设你有 ...
- DPDK的代码规范
每个公司都会有自己代码风格或者编程规范,都旨在防范编程语言的一些陷阱或者提高代码效率,还有就是保持一致编码风格来提高代码可读性,方便code review: 或者说代码的一种美学,比如python也就 ...
- c# json 反序列化 泛型List 2行代码
List<EncyTable> list = new List<EncyTable>(); var jsonReqeust = "[{ENCY_ID:775,ENCY ...
- Java API操作ZooKeeper
创建会话 package org.zln.zk; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watch ...
- Citrix NetScaler HA(高可用性)解析
Citrix NetScaler HA(高可用性)解析 来源 https://www.iyunv.com/thread-172259-1-1.html 1.1 NetScaler高可用概述 我 ...
- [bzoj5287] [HNOI2018]毒瘤
题目描述 从前有一名毒瘤. 毒瘤最近发现了量产毒瘤题的奥秘.考虑如下类型的数据结构题:给出一个数组,要求支持若干种奇奇怪怪的修改操作(比如区间加一个数,或者区间开平方),并支持询问区间和.毒瘤考虑了n ...
- 70种简单常用的JS代码
1.后退 前进 <input type="button" value="后退" onClick="history.go(-1)&quo ...