[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 ...
随机推荐
- Jquery 替换全部花括号
js:var str="{aaa}123{aaa}";str.replace(/\{aaa\}/g, "111");
- ASP.NET Excel数据导出数据库
/// <summary> /// 根據gridview導出excel /// </summary> /// <param name="ctl"> ...
- 使用Xcode插件,让iOS开发更加便捷
在iOS开发过程中,写注释是一项必不可少的工作.这不仅有助于自己对代码整理回顾,而且提高了代码的可读性,让代码维护变得容易.但是,写注释又是一项枯燥的工作.我们浪费了大量的时间在输入/*,*,*/这样 ...
- jQuery 效果 - 淡入淡出
通过 jQuery,您可以实现元素的淡入淡出效果. 点击展示 淡入/淡出 面板 实例 jQuery fadeIn()演示 jQuery fadeIn() 方法. jQuery fadeOut()演示 ...
- Delphi动态创建组件,并释放内存
开发所用delphi版本是xe2,效果图如下: 代码如下: ---------------------------------------------------------------------- ...
- 根据CreateDirectory递归创建多级目录
分为MFC下的和非MFC下的两种,MFC路径是CString类型的,非MFC的路径是wstring类型的. 下面是MFC下的创建目录: void __fastcall RecursiveDirecto ...
- c++文件读写相关
在看C++编程思想中,每个练习基本都是使用ofstream,ifstream,fstream,以前粗略知道其用法和含义,在看了几位大牛的博文后,进行整理和总结: 这里主要是讨论fstream的内容: ...
- 一个小白对Arguments的理解
1.Arguments是一个类似数组的实参队列,译为:论据,论点,姑且译作参数(实参): 2.Arguments与形参保持不同的存储空间,只有当形参被赋值之后两者才会相等 3.传说在严格模式下函数内自 ...
- Canvas实现文字粒子化,并且绕轴旋转(完善)
1. 之前有放过一个初始版本,但是因为在旋转的时候,有比较大的瑕疵,造成每个点运动到端点后,出现类似撞击的感觉. 2. 所以本文对旋转作了些调整,运用类似水平方向的圆周运动 a. HTML代码,定义c ...
- CSS实现DIV三角形
本文内容收集来自网络 #triangle-up { width:; height:; border-left: 50px solid transparent; border-right: 50px s ...