[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.
Examples:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its vertical order traversal as:
[
[9],
[3,15],
[20],
[7]
]
Given binary tree [3,9,20,4,5,2,7],
_3_
/ \
9 20
/ \ / \
4 5 2 7
return its vertical order traversal as:
[
[4],
[9],
[3,5,2],
[20],
[7]
]
分析:
从根节点出发走的是一左一右或者一右一左的路径到达某节点,那么这个节点的列数可看做0;如果是两左,则为-2;为两右为2。
代码:
void dfs(TreeNode *node, int col, vector<vector<int> > &vleft, vector<vector<int> > &vright) {
if(!node)
return;
//在根节点右边
if(col > ) {
//列编号超出了vright边界,则扩展边界
while(col >= vright.size())
vright.push_back(vector<int> ());
vright[col].push_back(node->val);
}
//在根节点列左边或者中间
else {
//列编号超出了vleft边界,则扩展边界
while(-col >= vleft.size())
vleft.push_back(vector<int> ());
vleft[-col].push_back(node->val);
}
dfs(node->left, col - , vleft, vright);
dfs(node->right, col + , vleft, vright);
return;
}
vector<vector<int> > verticalorder(TreeNode *root) {
vector<vector<int> > vleft, vright;
dfs(root, , vleft, vright);
//翻转vleft,然后与vright拼接
reverse(vleft.begin(), vleft.end());
vleft.insert(vleft.end(), vright.begin() + , vright.end());
return vleft;
}
[Locked] Binary Tree Vertical Order Traversal的更多相关文章
- [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 ...
- 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 ...
- [Swift]LeetCode314. 二叉树的竖直遍历 $ 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 -- LeetCode
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
随机推荐
- startActivityForResult和setResult详解
http://www.cnblogs.com/lijunamneg/archive/2013/02/05/2892616.html startActivityForResult与startActivi ...
- 利用html+ashx实现aspx的功能
最近准备学习下ASP.NET,初期在网上看了些视频教程,准备将自己学习的东西整理整理,留着日后可以参考参考. 本文采用了html.ashx实现aspx,实现了一个最简单的动态网页效果,开发环境是VS2 ...
- maven发布的资源文件到tomcat项目下
问题:项目中有hibernate的资源文件,src/main/java和src/main/resources都有这些文件,当启动项目时发现出错.但是src/main/java已经修改好了, 经查tom ...
- 用PHP实现一个高效安全的ftp服务器(一)
摘要: 本文主要阐述使用PHP的swoole扩展实现ftp服务器,同时扩展ftp服务器个性化功能和安全性.真正实现一个自己完全掌控的ftp服务器,可以个性化定制的ftp服务器. 正文: FTP服务器想 ...
- 绘图quartz之阴影
//设置矩形的阴影 并在后边加一个圆 不带阴影 步骤: CGContextRef context = UIGraphicsGetCurrentContext(); ...
- Lua-C交互函数
lua_gettable(lua_State * , tableIndex) //获取表的在key位置的值 过程:tableIndex为表在栈的位置,例:-2为第二个位置 , 此时会弹(出)栈作为参数 ...
- Winform使用DevExpress的WaitDialogForm画面
使用了DevExpress的WaitDialogForm 在应用程序加载开始时新建一个线程,并将loading画面show起来,在应用程序画面弹出前将该线程终止. 代码: private DevExp ...
- springMVC整合memcached,以注解形式使用
睡不着,深夜写点博客.闲下来有一个月了,心里多少有点…… 在北京找工作一再受阻,这个时间点也不好找 再接再厉 之前没有用过memcached,没有什么实战经验,看了一些关于memcached的博客,写 ...
- yii2源码学习笔记(三)
组件(component),是Yii框架的基类,实现了属性.事件.行为三类功能,如果需要事件和行为的功能,需要继承该类. yii\base\Component代码详解 <?php /** * @ ...
- ecshop--加载初始化文件
define('IN_ECS', true);require(dirname(__FILE__) . '/../../includes/init.php'); 在开头要加入这两句文件才可以访问数据库以 ...