要求

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

示例

  • ["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. 第三单元总结——JML契约式编程

    OO第三单元博客作业--JML与契约式编程 OO第三单元的三次作业都是在课程组的JML规格下完成.完成作业的过程是契约式编程的过程:设计者完成规格设计,实现者按照规格具体实现.作业正确性的检查同样围绕 ...

  2. pwn题命令行解题脚本

    目录 脚本说明 脚本内容 使用 使用示例 参考与引用 脚本说明 这是专门为本地调试与远程答题准备的脚本,依靠命令行参数进行控制. 本脚本支持的功能有: 本地调试 开启tmux调试 设置gdb断点,支持 ...

  3. 【10.5NOIP普及模拟】sort

    [10.5NOIP普及模拟]sort 文章目录 [10.5NOIP普及模拟]sort 题目描述 输入 输出 输入输出样例 样例输入 样例输出 数据范围限制 解析 code 题目描述 小x和小y是好朋友 ...

  4. java面试-JVM调优和参数配置,如何查看JVM系统参数默认值

    一.JVM的参数类型: 1.标配参数: java -version java -help 2.X参数: -Xmixed 混合模式(先编译后执行) -Xint  解释执行 -Xcomp 第一次使用就编译 ...

  5. 软工热身——回顾&展望

    项目 内容 这个作业属于哪个课程 2021春季软件工程 (罗杰 任健) 这个作业的要求在哪里 第一次阅读作业 我在这个课程的目标是 从实践中学习软件工程相关知识(结构化分析和设计方法.敏捷开发方法.软 ...

  6. oo第四单元作业总结暨课程总结

    oo第四单元作业总结暨课程总结 一.本单元作业架构设计 本单元需要构建一个UML解析器,通过对输入的UML类图/顺序图/状态图的相关信息进行解析以供查询,其中课程组已提供输入整体架构及输入解析部分,仅 ...

  7. JavaWeb 补充(XML)

    XML 1. 概念:Extensible Markup Language 可扩展标记语言 可扩展:标签都是自定义的. <user>  <student> 功能: 存储数据   ...

  8. linux-shell 识别当前所使用的shell

    echo $SHELL 或者 echo $0

  9. kubernetes 查看cpu,内存使用情况

    kubectl top pod --all-namespaces kubectl top pod -n kubeflow

  10. java面试一日一题:java的类加载过程

    问题:请讲下java的类加载机制及过程 分析:该问题主要考察对JVM加载类的机制及过程,延申出的问题是为什么使用双亲委托模型,有什么办法可以打破双亲委托吗: 回答要点: 主要从以下几点去考虑, 1.类 ...