要求

  • 给定一棵二叉树,返回所有表示从根节点到叶子节点路径的字符串

示例

  • ["1->2->5","1->3"]

思路

  • 递归地返回左右子树到叶子节点的字符串

示例

 1 class Solution {
2 public:
3 vector<string> binaryTreePaths(TreeNode* root) {
4
5 vector<string> res;
6
7 if( root == NULL )
8 return res;
9
10 if( root->left == NULL && root->right == NULL){
11 res.push_back( to_string(root->val) );
12 return res;
13 }
14
15 vector<string> leftS = binaryTreePaths(root->left);
16 for( int i = 0 ; i < leftS.size() ; i ++ )
17 res.push_back( to_string(root->val) + "->" + leftS[i]);
18
19 vector<string> rightS = binaryTreePaths(root->right);
20 for( int i = 0 ; i < rightS.size() ; i ++ )
21 res.push_back( to_string(root->val) + "->" + rightS[i]);
22
23 return res;
24 }
25 };

相关

  • 113 Path Sum II
  • 129 Sum Root to Leaf Numbers

[刷题] 257 Binary Tree Paths的更多相关文章

  1. Leetcode题 257. Binary Tree Paths

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

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

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

  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. 257. Binary Tree Paths返回所有深度优先的遍历

    [抄题]: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tr ...

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

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

  6. 257. Binary Tree Paths

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

  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. (easy)LeetCode 257.Binary Tree Paths

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

  9. Java [Leetcode 257]Binary Tree Paths

    题目描述: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tr ...

随机推荐

  1. 运维趋势2019年总结,运维就是要做到"技多不压身"

    2019年 在互联网这个行业中对运维的要求越来越来越高,比如2015.16年的时候,运维只是做一些人力投入的事情,比如重启个服务,搭建一个nginx,mysql主从服务,简单的优化一下,就可以上线了, ...

  2. day-05-字典

    字典的初识 why: 列表可以存储大量的数据,但数据之间的关联性不强 列表的查询速度比较慢.数量越大查询越慢 what:容器型数据类型:dict how: 数据类型的分类(可变与不可变) 可变(不可哈 ...

  3. 系统编程-网络-tcp客户端服务器编程模型(续)、连接断开、获取连接状态场景

    相关博文: 系统编程-网络-tcp客户端服务器编程模型.socket.htons.inet_ntop等各API详解.使用telnet测试基本服务器功能 接着该上篇博文,咱们继续,首先,为了内容的完整性 ...

  4. Python简单实现杨辉三角

    n=input("请输入要打印的行数")n=int(n)for x in range(0,n+1): p=1 print(''.rjust(n-x),end="" ...

  5. BUAA_OO_第二单元

    BUAA_OO_2020_UNIT2 一.程序结构分析 第五次作业 UML & Mertrics ​ 电梯的调度问题,实质上就是任务的请求与分配问题,笔者在第五次作业中采用简单的"生 ...

  6. 1.jsp-out和response输出的区别

    jsp中代码: out.write("out输出1 <br/>"); out.write("out输出2 <br/>"); respon ...

  7. centos7 中静态IP地址的配置

    虚拟机中也可以像Windows系统那样从浏览器上下载文件,但在这之前,要必须保证虚拟机网络服务通畅.而配置网络服务其实就是在编辑网卡配置文件,具体步骤如下: 网卡配置文件位置:/etc/sysconf ...

  8. linux gcc命令参数

    gcc命令参数笔记 1. gcc -E source_file.c -E,只执行到预处理.直接输出预处理结果. 2. gcc -S source_file.c -S,只执行到汇编,输出汇编代码. 3. ...

  9. 2021S软件工程——个人阅读作业1

    2021S软件工程--个人阅读作业1 项目 内容 这个作业属于哪个课程 2021春季软件工程(罗杰 任建) 这个作业的要求在哪里 2021年软工-热身阅读作业 我在这个课程的目标是 了解并熟悉软件开发 ...

  10. 修正js跳转

    var urls = new Array();urls["pc"] = "./hp"; //pcurls["sj"] = "./h ...