给定一个二叉树,返回从根节点到叶节点的所有路径。
例如,给定以下二叉树:
   1
 /   \
2     3
 \
  5
所有根到叶路径是:
["1->2->5", "1->3"]

详见:https://leetcode.com/problems/binary-tree-paths/description/

Java实现:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> res=new ArrayList<String>();
if(root==null){
return res;
}
helper(root,"",res);
return res;
}
private void helper(TreeNode root,String out,List<String> res){
out+=String.valueOf(root.val);
if(root.left==null&&root.right==null){
res.add(out);
}
if(root.left!=null){
helper(root.left,out+"->",res);
}
if(root.right!=null){
helper(root.right,out+"->",res);
}
}
}

C++实现:

/**
* 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:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
if(root)
{
helper(root,"",res);
}
return res;
}
void helper(TreeNode *root,string out,vector<string> &res)
{
out+=to_string(root->val);
if(!root->left&&!root->right)
{
res.push_back(out);
}
if(root->left)
{
helper(root->left,out+"->",res);
}
if(root->right)
{
helper(root->right,out+"->",res);
}
}
};

参考:https://www.cnblogs.com/grandyang/p/4738031.html

257 Binary Tree Paths 二叉树的所有路径的更多相关文章

  1. 【easy】257. Binary Tree Paths 二叉树找到所有路径

    http://blog.csdn.net/crazy1235/article/details/51474128 花样做二叉树的题……居然还是不会么…… /** * Definition for a b ...

  2. [leetcode]257. Binary Tree Paths二叉树路径

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

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

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

  4. Leetcode 257 Binary Tree Paths 二叉树 DFS

    找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...

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

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

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

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

  7. 【LeetCode】257. Binary Tree Paths

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

  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. Note: A leaf is a node with no children. Example ...

随机推荐

  1. CDOJ1339 郭大侠与线上游戏(维护一个set中的中位数——平衡树/双set)

    http://acm.uestc.edu.cn/#/problem/show/1339 题意:有三种操作,分别是. 1.向队列推入一个数x. 2.弹出这个队列的第一个数字 3.查询这个队列的中位数是多 ...

  2. 看我如何基于Python&Facepp打造智能监控系统

    由于种种原因,最近想亲自做一个基于python&facepp打造的智能监控系统. 0×00:萌芽 1:暑假在家很无聊 想出去玩,找不到人.玩个lol(已卸载),老是坑人.实在是无聊至极,不过, ...

  3. 微信小程序 wafer2框架摘要

    微信小程序 wafer2框架摘要 帮助文档:https://github.com/tencentyun/wafer2-startup/wiki 使用了knex.js进行数据库交互,使用了koa.js进 ...

  4. modem&NIC&sound card

    Rate: Phone:8 k hz   radio:22050 hz Digital Video camcorder; miniDV; DAT LP mode:32 k hz Audio CD MP ...

  5. psychology

    壹.自身(荣.命)   一.职业分析   ㈠.分析性格→分析长处和短处→分析大家都有的长处→确定自己最终发展的专业.   1 .性格--宗正   耐压力特强,即使肩头责任重大,也能够处理得稳稳当当,是 ...

  6. yarn之安装依赖包

    安装依赖关系 yarn install用于安装项目的所有依赖项.依赖关系从您的项目package.json文件中检索,并存储在yarn.lock文件中. 开发包时,安装依赖关系最常见的是在 您刚刚检出 ...

  7. NoSQL数据库概览及其与SQL语法的比較

    [文章摘要] HBase是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统.同一时候也是知名的NoSQL数据库之中的一个.NoSQL数据库的产生就是为了解决大规模数据集合多重数据种类带来的挑战,尤 ...

  8. scu oj 4442 Party(2015年四川省acm程序设计竞赛)

    Party n frogs are invited to a tea party. Frogs are conveniently numbered by 1,2,-,n. The tea party ...

  9. 利用runtime检測这个对象是否存在某属性?

    假定有实例对象-instance,我们怎样知道该实例对象是否存在属性-propertyName? 利用runtime,我们能够获取到它的属性列表 1)属性列表 unsigned int outCoun ...

  10. 【codeforces379F】 New Year Tree

    距离一个点最远的点一定是直径的一个端点.考虑运用这个原理,每次维护一下直径端点即可. #include<algorithm> #include<iostream> #inclu ...