Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >LeetCode 0257 - Binary Tree Paths

LeetCode 0257 - Binary Tree Paths

作者头像
Reck Zhang
发布于 2021-08-11 04:07:28
发布于 2021-08-11 04:07:28
23500
代码可运行
举报
文章被收录于专栏:Reck ZhangReck Zhang
运行总次数:0
代码可运行

Binary Tree Paths

Desicription

Given a binary tree, return all root-to-leaf paths.

Note: A leaf is a node with no children.

Example:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input:

   1
 /   \
2     3
 \
  5

Output: ["1->2->5", "1->3"]

Explanation: All root-to-leaf paths are: 1->2->5, 1->3

Solution

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
/**
 * 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 {
private:
    vector<string> res{};
    void search(TreeNode* root, string path) {
        if(root == nullptr) {
            return ;
        }
        path += path == "" ? to_string(root->val) : "->" + to_string(root->val);
        if(root->left == nullptr && root->right == nullptr) {
            res.push_back(path);
            return ;
        }
        if(root->left) {
            search(root->left, path);
        }
        if(root->right) {
            search(root->right, path);
        }
    }
public:
    vector<string> binaryTreePaths(TreeNode* root) {
        search(root, "");
        return res;
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-12-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Leetcode 257. Binary Tree Paths
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"] 求二叉树所有从根到叶的路径。 简单DFS /** * Definition for a binary tree node. * struct TreeN
triplebee
2018/01/12
5060
Baozi Training Leetcode solution 257: Binary Tree Paths
Blogger:https://blog.baozitraining.org/2019/08/leetcode-solution-257-binary-tree-paths.html
包子面试培训
2019/09/17
4080
Tree - 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths.
ppxai
2020/09/23
4320
LeetCode笔记:257. Binary Tree Paths
image.png All root-to-leaf paths are: ["1->2->5", "1->3"]
Cloudox
2021/11/23
1660
LeetCode笔记:257. Binary Tree Paths
leetcode257 Binary Tree Paths
题目 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”] 需要得到所有从根到叶子结点的路径。 解题思路 既然是得到路径,那么很自然的想到使用深度优先遍历(DFS),DFS在二叉树中对应的就是前序遍历,然后用一个
用户1665735
2018/06/20
3730
利用递归函数的返回值
路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点)。
宇宙之一粟
2020/10/26
1.7K0
Binary Tree Paths
1. Description 2. Solution Recursive /** * Definition for a binary tree node. * struct TreeNode {
Tyan
2019/05/25
3220
LeetCode 0113 - Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.
Reck Zhang
2021/08/11
2340
LeetCode 257. Binary Tree Paths
题目 /** * 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> ans; vect
ShenduCC
2020/04/21
2400
【leetcode刷题】T133-二叉树的所有路径
https://leetcode-cn.com/problems/binary-tree-paths/
木又AI帮
2019/08/06
4600
二叉树的所有路径(C++)
给你一个二叉树的根节点 root,按 任意顺序 ,返回所有从根节点到叶子节点的路径。
GeekLiHua
2025/01/21
710
二叉树的所有路径(C++)
Leetcode 113 Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4
triplebee
2018/01/12
5260
LeetCode 0129 - Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.
Reck Zhang
2021/08/11
2100
LeetCode 0107 - Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).
Reck Zhang
2021/08/11
1570
DFS、异或-LeetCode 257、258、268、283
使用dfs算法获得每条路径,注意在生成字符串时可以使用string类型,可以直接使用加法操作对字符串进行拼接得到最终结果!
算法工程师之路
2019/11/26
3590
LeetCode 二叉树 题目分类汇总
简书的 markdown 都不支持 [TOC] 语法……我就不贴目录了。下面按照类别,列出了29道关于二叉树的题目。认真看会发现,其实题目核心思想都是DFS(如果使用的话),并且题目的类型并不多。
Yano_nankai
2018/10/08
1.4K0
Leetcode|二叉树的属性|257. 二叉树的所有路径
这块其实要多维护一个记录每个节点下累计路径的队列,该队列和数的广度优先队列同步进出数据,当遇到叶节点时,原路径向量把当前叶节点对应的累计路径进行存储即可。
SL_World
2021/09/18
2400
LeetCode 113 Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. Note: A leaf is a node with no children. Example: Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ /
ShenduCC
2018/07/05
3780
Leetcode 129 Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 /
triplebee
2018/01/12
4530
二叉树构建与遍历-LeetCode 889、1008、129、113
返回与给定的前序和后序遍历匹配的任何二叉树。 pre 和 post 遍历中的值是不同的正整数。
算法工程师之路
2019/11/26
5540
相关推荐
Leetcode 257. Binary Tree Paths
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验