题目:

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 RootPath(vector<string>& resultVec, TreeNode *root, string s)
{
if (root->left == NULL && root->right == NULL)
{
resultVec.push_back(s);
return;
}
if (root->left)
{
RootPath(resultVec, root->left, s + "->" + to_string(root->left->val));
}
if (root->right)
{
RootPath(resultVec, root->right, s + "->" + to_string(root->right->val));
}
} vector<string> binaryTreePaths(TreeNode* root) {
vector<string> result;
if (root == NULL) return result;
RootPath(result, root, to_string(root->val));
return result;
}
};

[LeetCode257]Binary Tree Paths的更多相关文章

  1. [LintCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 /   \2 ...

  2. LintCode Binary Tree Paths

    Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Given the following binary tre ...

  3. 【LeetCode】257. Binary Tree Paths

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

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

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

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

  6. [Swift]LeetCode257. 二叉树的所有路径 | Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

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

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

  8. leetcode : 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. fusionchart实现ZoomLine 源码 破解版 能够导出

    近期画油量曲线须要用到ZoomLine官网看了好几天.如今整理出来供大家參考使用 zoomline.html源码 <!DOCTYPE HTML PUBLIC "-//W3C//DTD ...

  2. 【ArcGIS 10.2新特性】ArcGIS 10.2 for Server新特性

    ArcGIS forServer相关的很多重大消息,如与Portal for ArcGIS进行了集成,提供对实时数据支持.离线地图使用. 1.与Portal for ArcGIS集成 用户能够配置Po ...

  3. WebService 通过POST方式访问时候,因 URL 意外地以“/方法名”结束,请求格式无法识别 解决办法

    因URL意外地以“/方法名”结束,请求格式无法识别. 执行当前Web请求期间生成了未处理的异常.可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息. 解决方法:在webservice的we ...

  4. java nio-理解同步、异步,阻塞和非阻塞

    理解同步.异步,阻塞和非阻塞 结论:阻塞.非阻塞与是否同步异步无关.     转自知乎 “阻塞”与"非阻塞"与"同步"与“异步"不能简单的从字面理解, ...

  5. 利用objc的runtime来定位次线程中unrecognized selector sent to instance的问题

    昨天遇到一个仅仅有一行错误信息的问题: -[NSNull objectForKey:]: unrecognized selector sent to instance 0x537e068 因为这个问题 ...

  6. 第一个Python程序的Hello Python,竟然有问题

    print 'hello python' 运行时显示:SyntaxError: invalid syntax 解决办法: 这应该是版本的问题,Python2的话直接就可以输出,但是到了Python3需 ...

  7. Javascript异步数据的同步处理方法

    数据处理方法封装 var DataWatch=(function(){ var gWatch={},cursor= 0,callback_key = 'callback',gMap={}; var c ...

  8. Mongodb数据备份恢复

    Mongodb数据备份恢复 一.MongoDB数据库导入导出操作 1.导出数据库 twangback为备份的文件夹 命令: mongodump -h 127.0.0.1[服务器IP] -d advie ...

  9. MySQL命令行数据操作使用心得(总结版)

    Char 0~255 Varchar 0~65535 text 0~65535(只能保存字符) Longtext 0~4294967295(只能保存字符) CMD登陆mysql mysql -u ro ...

  10. SignalR技术

    Asp.net SignalR快速入门 一.前言 之前半年时间感觉自己有点浮躁,导致停顿了半年多的时间没有更新博客,今天重新开始记录博文,希望自己可以找回初心,继续沉淀.由于最近做的项目中用到Sign ...