给定一个二叉树,返回所有从根节点到叶子节点的路径。

说明: 叶子节点是指没有子节点的节点。

示例:

输入:

   1
/ \
2 3
\
5

输出: ["1->2->5", "1->3"]

解释: 所有根节点到叶子节点的路径为: 1->2->5, 1->3

class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
if(root == NULL)//二叉树为空
return vec;
DFS(root, to_string(root->val));
return vec;
} void DFS(TreeNode* root, string str) {
if (root->right == NULL && root->left == NULL) {//搜索完一个叶子节点,将数据存入容器
vec.push_back(str);
return;
}
if(root->left != NULL)//防止越界取值
DFS(root->left, str + "->" + to_string(root->left->val));//先将左子叶遍历,使用str存储递归中经过的值
if(root->right != NULL)
DFS(root->right, str + "->" + to_string(root->right->val));
}
private:
vector<string> vec;
};

  

转载:这个会更好理解

/*这个应该挺容易理解*/
class Solution {
private:
vector<string> ans;// 最终的解答
public:
vector<string> binaryTreePaths(TreeNode* root) {
binaryTreePaths(root, "", true);// 递归求解
return ans;
}
private:
void binaryTreePaths(TreeNode* root, string s, bool isRoot) {
if (!root) return;
s += (isRoot ? "" : "->") + to_string(root->val);//根节点需要特殊处理
if (!root->left && !root->right) {// 如果找到一个叶子节点
ans.push_back(s);
return;
}
binaryTreePaths(root->left, s, false);
binaryTreePaths(root->right, s, false);
}
};

LeetCode 257.二叉树所有路径(C++)的更多相关文章

  1. Java实现 LeetCode 257 二叉树的所有路径

    257. 二叉树的所有路径 给定一个二叉树,返回所有从根节点到叶子节点的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 输入: 1 / \ 2 3 \ 5 输出: ["1->2 ...

  2. LeetCode 257 二叉树的所有路径

    题目: 给定一个二叉树,返回所有从根节点到叶子节点的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 输入: 1 / \ 2 3 \ 5 输出: ["1->2->5&quo ...

  3. leetcode 257. 二叉树的所有路径 包含(二叉树的先序遍历、中序遍历、后序遍历)

    给定一个二叉树,返回所有从根节点到叶子节点的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 输入: 1 / \2 3 \ 5 输出: ["1->2->5", & ...

  4. 【Leetcode】二叉树简单路径最大和问题

    问题一:二叉树任意两个叶子间简单路径最大和 示例: -100 /   \ 2   100 /  \ 10   20 思路:这个问题适用于递归思路. 首先,将问题简单化:假设包含最大和summax的简单 ...

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

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

  6. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  7. [LeetCode] Path Sum IV 二叉树的路径和之四

    If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...

  8. [LeetCode] 666. Path Sum IV 二叉树的路径和 IV

    If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...

  9. [LeetCode] 112. Path Sum 路径和

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

随机推荐

  1. java 七牛上传图片到服务器(采用的html5 压缩 传输base64方式)

    //html 页面如下<div class="form-group"> <label class="col-sm-2 control-label&quo ...

  2. python的requests库怎么发送带cookies的请求

     背景: 在用robot做接口自动化时,有一个查询接口需要用到登录后返回的token等信息作为cookies作为参数一起请求(token是在返回体中,并不在cookies中), 刚好create se ...

  3. C#ADO.NET基础二

    DataAdapter的使用,批量增删改 1.使用DataAdapter查询 private void Select2() { try { using (SQLiteConnection conn = ...

  4. 仓储(Repository)和工作单元模式(UnitOfWork)

    仓储和工作单元模式 仓储模式 为什么要用仓储模式 通常不建议在业务逻辑层直接访问数据库.因为这样可能会导致如下结果: 重复的代码 编程错误的可能性更高 业务数据的弱类型 更难集中处理数据,比如缓存 无 ...

  5. ecliplse集成SVN

    按照下图操作 : SVN不同版本下载链接在文章底部有提供 上图点击add之后稍等一会就会弹出下图: 上图点击next之后: 最后等待完成之后重启ecliplse即可 重启ecliplse之后显示SVN ...

  6. C# LINQ(3)

    我们还是接着讨论一下group by 这一章节讨论group的本质:分组. 分组之后进行存储或者查询. 这个时候就要用一个新的关键字:into 这个之后就group就不作为结尾了. 必须重写另起sel ...

  7. React + Dva + Antd + Umi 概况

    Dva 由阿里架构师 sorrycc 带领 team 完成的一套前端框架,在作者的 github 里是这么描述它的:"dva 是 react 和 redux 的最佳实践". Ant ...

  8. 移动端页面怎么适配ios页面

    1.viewport 简单粗暴的方式:<meta name="viewport" content="width=320,maximum-scale=1.3,user ...

  9. SpringMVC中web.xml的配置(直接coppy使用就行)

    <?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://w ...

  10. vue中axios的使用与封装

    分享下我自己的axios封装axios是个很好用的插件,都是一些params对象,所以很方便做一些统一处理 当然首先是npm安装axios 很简单$ npm install axios --save在 ...