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 ...
随机推荐
- ACM题目————士兵杀敌(三)
[RMQ算法]:用于当数组过于庞大的时候,查询区间的最大(最小)值. 时间复杂度:O(nlogn),主要时间发费在预处理上,查询只要O(1). 描述 南将军统率着N个士兵,士兵分别编号为1~N,南将军 ...
- Unity5 Standard自发光材质无效解决方法
受光物和发光物都要勾选Static才会有效. 如果不够选静态就是用HDR光照,需要相机勾选HDR 所以如果是动态的物体真实发光,Unity依旧无法支持
- reactor模式学习
一.介绍reactor模式 二.使用reactor模式 三.参考 http://blog.csdn.net/swordmanwk/article/details/6170995 该文章,简单介绍了r ...
- google prettify 代码高亮显示
引入js和css文件 下载地址 http://files.cnblogs.com/jaday/prettify.zip js文件代码 !function(){var q=null;window.PR_ ...
- html的<marquee></marquee>标签实现滚动效果
页面的自动滚动效果,可由javascript来实现,但是今天无意中发现了一个html标签 - <marquee></marquee>可以实现多种滚动效果,无需js控制. 使用m ...
- 对table的tr使用display:block显示colspan失效问题的解决
qqqq <table> <tr> <td id="qqq" colspan="3" style="display:no ...
- 2016年11月13日 星期日 --出埃及记 Exodus 20:4
2016年11月13日 星期日 --出埃及记 Exodus 20:4 "You shall not make for yourself an idol in the form of anyt ...
- java实现UDP协议传输DatagramSocket
摘自:http://blog.csdn.net/wintys/article/details/3525643/ Server端. package com.topca.server; import ja ...
- Jnotify文件监控的用法以及Jar文件导入的方法
简介Jnotiy, 支持动态监控(支持级联监控)文件夹和文件的jar包.在linux中,调用linux底层的jnotify服务.在windows中,需要添加附件的dll文件. 因为通用的Maven仓库 ...
- J2EE开发规范
J2EE开发规范一 JAVA编码规范1 命名规范1.1 包命名 包名称必须全部用小写. 命名方式:业务领域名.公司名.项目名.模块名 如com.yr.xxx.dao.1.2 类命名类名以英文单词取 ...