Binary Tree Paths
Description:
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
1
/ \
2 3
\
5
All root-to-leaf paths are:
["1->2->5", "1->3"]
Code:
void allPath(TreeNode*root, vector<string>&result, vector<int>&path)
{
if (root==NULL)
return ;
path.push_back(root->val);
if (root->left==NULL && root->right==NULL)
{
//注意这里要用ostringstream将字符串写到流中去,然后用str()函数返回字符串
ostringstream sstr;
for (int i = ; i < path.size()-; ++i)
sstr << path[i]<<"->";
sstr<<path[path.size()-];
result.push_back(sstr.str());
}
else
{
if (root->left)
allPath(root->left, result, path);
if (root->right)
allPath(root->right, result, path);
}
path.pop_back();
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<string>result;
vector<int>path;
allPath(root, result,path);
return result;
}
Binary Tree Paths的更多相关文章
- [LintCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 / \2 ...
- LintCode Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Given the following binary tre ...
- 【LeetCode】257. Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
- <LeetCode OJ> 257. Binary Tree Paths
257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...
- LeetCode_257. Binary Tree Paths
257. Binary Tree Paths Easy Given a binary tree, return all root-to-leaf paths. Note: A leaf is a no ...
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- leetcode : Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- Leetcode 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- (easy)LeetCode 257.Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
随机推荐
- rtc关机闹钟7 jni层 com_android_server_AlarmManagerService
frameworks/base/services/core/jni/com_android_server_AlarmManagerService.cpp int AlarmImplAlarmDrive ...
- YTU 2989: 顺序表基本运算(线性表)
2989: 顺序表基本运算(线性表) 时间限制: 1 Sec 内存限制: 128 MB 提交: 1 解决: 1 题目描述 编写一个程序,实现顺序表的各种基本运算(假设顺序表的元素类型为char), ...
- 发现一个jq的问题
用jq对checkbox的checked属性进行操作时,使用$(‘#id’).attr(‘checked’, true);竟然无效,改成$(‘#id’).prop(‘checked’, true);才 ...
- (POJ2635)The Embarrassed Cryptographer(大数取模)
The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13041 Accep ...
- linux内核2.4.x网络接口分析层次图
http://blog.csdn.net/wswifth/article/details/5108744 今天大概分写了下<Linux内核2.4.x的网络接口源码的结构[转]>中的结构层次 ...
- Resilio(BtSync)搭建
Resilio(原名:BtSync)介绍 同步是使用PC和Mac,NAS,甚至服务器之间传输文件的最好方法.创建自己的私有云.连接设备和同步文件安全,不发送他们在第三方服务器.我们不限制你的速度和存储 ...
- Linux下统计出现次数最多的指定字段值
假设桌面上有一个叫“data.txt”的文本,内容如下: {id='xxx' info='xxx' kk='xxx' target='111111' dd='xxx'}{id='xxx' info=' ...
- 浅谈Apache性能调优
做了很多WEB系统性能测试,都知道了解测试环境,服务器硬件配置,web服务器参数配置是我们开始测试前首先要做的事情. 针对并发数量来说,不同数量级的用户并发,需求的服务器和web服务参数肯定是不同的. ...
- Codeforces Round #373 (Div. 2) A
Description Every summer Vitya comes to visit his grandmother in the countryside. This summer, he go ...
- 2016年10月22日 星期六 --出埃及记 Exodus 19:6
2016年10月22日 星期六 --出埃及记 Exodus 19:6 you will be for me a kingdom of priests and a holy nation.' These ...