题目:

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:
    vector<string> binaryTreePaths(TreeNode* root) {
        if(root == NULL)
            return res;
        stringstream ss;
        ss << root -> val;
        if((root -> left == NULL) && (root -> right == NULL))
        {
            res.push_back(ss.str());
            return res;
        }
        else
        {
            getPaths(root,ss.str());
        }
        return res;
    }
   
    void getPaths(TreeNode *root,string s)
    {
        if((root -> left == NULL) && (root -> right ==NULL))
        {
            res.push_back(s);
        }
        if(root -> left != NULL)
        {
            stringstream ss;
            ss << s << "->" << root -> left -> val;
            getPaths(root -> left , ss.str());
        }
        if(root -> right != NULL)
        {
            stringstream ss;
            ss << s << "->" <<  root -> right -> val;
            getPaths(root -> right , ss.str());
        }
    }
private:
    vector<string> res;
};

转载于:https://www.cnblogs.com/CodingGirl121/p/5440096.html

Leetcode题目: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. LeetCode OJ -- Binary Tree Paths

    http://blog.ubooksapp.com/ 标签(空格分隔): LeetCode OJ BinaryTree Given a binary tree, return all root-to- ...

  3. LeetCode 257 Binary Tree Paths

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

  4. [LeetCode] Binary Tree Paths - 二叉树基础系列题目

    目录: 1.Binary Tree Paths - 求二叉树路径 2.Same Tree - 判断二叉树相等 3.Symmetric Tree - 判断二叉树对称镜像 Binary Tree Path ...

  5. LeetCode刷题记录14——257. Binary Tree Paths(easy)

    LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...

  6. Binary Tree Paths leetcode

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

  7. LeetCode: 107. Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  8. LeetCode: 103. Binary Tree Zigzag Level Order Traversal

    题目 Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left ...

  9. LeetCode: Flatten Binary Tree to Linked List

    LeetCode: Flatten Binary Tree to Linked List LeetCode: Flatten Binary Tree to Linked List Given a bi ...

  10. CF750G New Year and Binary Tree Paths(数位dp二进制+数学)

    CF750G New Year and Binary Tree Paths description solution code description 题目链接 一颗无穷个节点的完全二叉树. 求有多少 ...

最新文章

  1. python要点简要-一日精通python
  2. mac安装mysql修改密码_Mac下安装MySQL 5.7.28并且修改root密码-Go语言中文社区
  3. 包python_Python 包:
  4. npm介绍以及常用命令
  5. 2014年 第5届 蓝桥杯 Java B组 省赛解析及总结
  6. Myeclipse使用DB Browser连接数据库错误:OPTION SQL_SELECT_LIMIT=DEFAULT
  7. 请解释Spring Bean 的生命周期?
  8. LuoguP5504 [JSOI2011]柠檬
  9. wangEditor-3.1.1 自己扩展的,增加代码块行号
  10. nemesis什么车_马力2100匹《Trion Nemesis》谜样超跑诞生中?
  11. rust和gta5哪个吃配置_晨报:Steam周销量榜 曝次世代GTA5用大镖客2引擎
  12. 移动办公之路的行业探索
  13. MySQL 索引最左匹配原则的理解
  14. NI视觉视觉软件简介
  15. ROC(AUC)的显著性检验
  16. 浏览器设置代理服务器插件SwitchyOmega配置
  17. 简单枚举(ZJM要抵御宇宙射线)
  18. excel这几大数据处理技巧,高效率操作技能,今天免费交给你!
  19. Android---RecyclerView网络请求图片加载
  20. Unity - URP RenderFeature - 实现类似多 Pass 的 XRay: Rim、Pattern

热门文章

  1. 【随感】我觉得,世界上最美好的乐器是钢琴和架子鼓
  2. JavaScript笔记1基础
  3. 不是我发现了美,只不过这个世界本身就很美。记快乐的今年生日。
  4. [Web 前端] 005 html 常用标签补充
  5. bash shell for循环1到100
  6. 'System.Data.SqlClient' could not be loaded解决办法
  7. 网络爬虫(2)--异常处理
  8. 第29章:字符串编辑距离
  9. Oracle Net
  10. 如何关闭父窗体?C#(已解决)