[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 ...
随机推荐
- sql语句中like的使用
先看一道题: 写出一条sql语句,找出表B中 字段Value中不全是字母 数字 下划线的数据 初看这道题,我们想到可以用like去进行模糊匹配,找出想要的结果.但是有一个地方需要注意:如果想在SQL ...
- ado.net与各种orm操作数据方式的比较
ADO.NET与ORM的比较(1):ADO.NET实现CRUD http://zhoufoxcn.blog.51cto.com/792419/283952 ADO.NET与ORM的比较(2):NHib ...
- Html5-Canvas实现简易的抽奖转盘
###Html5实现抽奖转盘效果 1.实现的基本效果 2.主要的内容 html5中canvas标签的使用 jQueryRotate.js旋转插件 3.主要html代码 <body> < ...
- JavaIO流——File类
1.掌握File 类的作用 2.可以使用File 类中的方法对文件进行操作 所有的 io 操作都保存在 java.io 包中. 构造方法:public File (String pathname) 直 ...
- Linux 使用yum install安装mysql登陆不上解决办法
CentOS yum安装mysql后 Can’t connect to local MySQL server through socket ‘/var/lib/ CentOS Can’t connec ...
- JavaScript不可变原始值和可变的对象引用
一.JavaScript不可变原始值 JavaScript中的原始值(undefined,null,布尔值,数字和字符串)与对象(包括了数组和函数)有着根本的区别.原始值是不可变的(undefined ...
- 长期支持版本(即不自动更新版本) - Flash Player 18.0.0.268
无意中发现,适合不喜欢折腾的朋友. 下载链接:(官方:http://www.adobe.com/cn/products/flashplayer/distribution3.html) (分流:http ...
- 黑马程序员—C语言的特点和关键字
------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- C语言的简介 一. C语言具有下列特点: C语言既具有低级语言直接操纵硬件的特点,又具有高级语言 ...
- Windows 10正式版密钥大全,Win10激活序列号KEY大全
最新放出来的Win10密钥:NJ4MX-VQQ7Q-FP3DB-VDGHX-7XM87 MH37W-N47XK-V7XM9-C7227-GCQG9 VK7JG-NPHTM-C97JM-9MPGT-3V ...
- bzoj2096: [Poi2010]Pilots
Description Tz又耍畸形了!!他要当飞行员,他拿到了一个飞行员测试难度序列,他设定了一个难度差的最大值,在序列中他想找到一个最长的子串,任意两个难度差不会超过他设定的最大值.耍畸形一个人是 ...