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"]
代码如下:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
//按照容易自己习惯的数据结构来存储路径,最后做一个转换
void findPath(TreeNode* t,vector<int>& temp, vector<vector<int> >& record)
{
temp.push_back(t->val);
if(t->left!=NULL) findPath(t->left, temp, record);
if(t->right!=NULL) findPath(t->right, temp, record);
//到达叶子节点,就记录路径
if(t->left==NULL && t->right==NULL)
{
record.push_back(temp);
temp.erase(temp.end()-1);
return ;
}
//中间节点返回
temp.erase(temp.end()-1);
return;
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<vector<int>> record;
vector<string> srecord;
if(root==NULL) return srecord;
vector<int> temp;
//找到所有的路
findPath(root,temp,record);
//把所有的路按照题目要求存储
string stemp;
for(int i=0;i<record.size();i++)
{
stringstream ss;
for(int j=0;j<record[i].size();j++)
{
ss<<record[i][j]<<"->";
}
stemp.clear();
stemp=ss.str();
//去掉最后的"->"
stemp.pop_back();
stemp.pop_back();
srecord.push_back(stemp);
}
return srecord;
}
};
												

Leetcode题 257. Binary Tree Paths的更多相关文章

  1. 【LeetCode】257. Binary Tree Paths

    Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...

  2. 【LeetCode】257. Binary Tree Paths 解题报告(java & python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...

  3. 【一天一道LeetCode】#257. Binary Tree Paths

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. LeetCode OJ 257. Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  5. [刷题] 257 Binary Tree Paths

    要求 给定一棵二叉树,返回所有表示从根节点到叶子节点路径的字符串 示例 ["1->2->5","1->3"] 思路 递归地返回左右子树到叶子节 ...

  6. &lt;LeetCode OJ&gt; 257. Binary Tree Paths

    257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...

  7. LeetCode 257. Binary Tree Paths (二叉树路径)

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  8. [LeetCode] 257. Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  9. Leetcode 257. Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

随机推荐

  1. Python Scrapy 实战

    Python Scrapy 什么是爬虫? 网络爬虫(英语:web crawler),也叫网络蜘蛛(spider),是一种用来自动浏览万维网的网络机器人.其目的一般为编纂网络索引. Python 爬虫 ...

  2. Java集合框架介绍。Java Collection Frameworks = JCF

    Java集合框架 = Java Collection Frameworks  = JCF . 为了方便理解,我画了一张思维脑图.

  3. nginx 请求限制和访问控制

    请求限制 限制主要有两种类型: 连接频率限制: limit_conn_module 请求频率限制: limit_req_module HTTP协议的连接与请求 HTTP协议是基于TCP的,如果要完成一 ...

  4. Linux 知识

    linux下如何查看某软件是否已安装 因为linux安装软件的方式比较多,所以没有一个通用的办法能查到某些软件是否安装了.总结起来就是这样几类: 1.rpm包安装的,可以用rpm -qa看到,如果要查 ...

  5. Oracle查看占用表空间最大的表(排序)

    场景:在做数据库巡检时,检查大表是必不可少的操作,可以查看各表占用表空间的大小 代码: as sizes,q.num_rows,t.segment_type from dba_segments t l ...

  6. 元类编程--__get__ __set__属性描述符

    from datetime import date, datetime import numbers class IntField: #数据描述符,实现以下任意一个,都会变为属性描述符 def __g ...

  7. Dymola — 多学科系统仿真平台

            Dymola 是法国Dassault Systems公司的多学科系统仿真平台,广泛应用于国内外汽车.工业.交通.能源等行业的系统总体架构设计.指标分解以及系统功能验证及优化等.Dymo ...

  8. html知识补充

    1.点击超链接跳转到新窗口 <a href="http://www.baidu.com" target="_blank">百度一下</a> ...

  9. Wide & Deep Learning for Recommender Systems

    Wide & Deep Learning for Recommender Systems

  10. 《The one!》团队作业五:团队项目需求改进与系统设计

    项目 内容 作业所属课程 所属课程 作业要求 作业要求 团队名称 < The One !> 作业学习目标 (1)掌握面向对象需求分析方法:(2)学习软件系统总体结构和数据库逻辑结构设计,学 ...