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的更多相关文章

  1. [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  2. LeetCode Binary Tree Vertical Order Traversal

    原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, ...

  3. LeetCode 314. 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 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  5. Binary Tree Vertical Order Traversal

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  6. 314. Binary Tree Vertical Order Traversal

    题目: Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to ...

  7. [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 ...

  8. [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 ...

  9. Binary Tree Vertical Order Traversal -- LeetCode

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

随机推荐

  1. ashx页面 “检测到有潜在危险的 Request.Form 值”的解决方法(控制单个处理程序不检测html标签)

    如题: 使用web.config的configuration/location节点. 在configuration节点内新建一个location节点,注意这个节点和system.webserver那些 ...

  2. Missing iOS Distribution signing identity问题解决

    问题描述 打包上传APPStore  Xcode报以下错误:Missing iOS Distribution signing identity for XXXXXX 查看证书后发现,Develop证书 ...

  3. Objective-C的对象模型 http://foredoomed.org/blog/2014/02/24/object-modeling-of-objective-c/

    Objective-C是一门面向对象,并且在C的基础上加入了Smalltalk式的消息机制而形成的编程语言,它主要被苹果公司用于开发Mac OS X和iOS操作系统.既然Objective-C是面向对 ...

  4. Java study 2:The note of studying Socket which based TCP

    TCP concept: 传输控制协议(Transmission Control Protocol, TCP)是一种面向连接(连接导向)的.可靠的.基于字节流的运输层(Transport layer) ...

  5. mahout的安装、配置及运行java程序

    一.下载安装包: http://mahout.apache.org/general/downloads.html 二.解压: 将下载的安装包解压到需要的目录下 三.配置环境变量: export MAH ...

  6. SGU 114.Telecasting station

    题意: 百慕大的每一座城市都坐落在一维直线上.这个国家的政府决定建造一个新的广播电视台.经过了许多次试验后,百慕大的科学家们提出了一个结论,在每座城市的不满意度等于这座城市的市民数与这座城市与广播电视 ...

  7. printf 格式化最常用用法

    printf 操作符的参数包括”格式字符串“及”要输出的数据列表". 格式字符串好像用来填空的模版,代表你想要的输出格式: printf "Hello,%s;your passwo ...

  8. 初探grunt.js

    package.js { "name": "ttd_v3", "version": "0.1.0", "aut ...

  9. C# Windows服务安装出现System.Security.SecurityException异常解决办法

    我把注册windows服务所用的安装及启用服务命令写到了bat可执行文件(名称为install.bat)中,如下所示: %SystemRoot%\Microsoft.NET\Framework\v4. ...

  10. 对象的内置属性和js的对象之父Object()

    js中对象有constructor,valueOf(),toString()等内置属性和方法; 创建一个空对象的方法: var o = {}; 或者 var o= new Object(); o.co ...