LeetCode 257.二叉树所有路径(C++)
给定一个二叉树,返回所有从根节点到叶子节点的路径。
说明: 叶子节点是指没有子节点的节点。
示例:
输入:
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++)的更多相关文章
- Java实现 LeetCode 257 二叉树的所有路径
		257. 二叉树的所有路径 给定一个二叉树,返回所有从根节点到叶子节点的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 输入: 1 / \ 2 3 \ 5 输出: ["1->2 ... 
- LeetCode 257 二叉树的所有路径
		题目: 给定一个二叉树,返回所有从根节点到叶子节点的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 输入: 1 / \ 2 3 \ 5 输出: ["1->2->5&quo ... 
- leetcode    257. 二叉树的所有路径  包含(二叉树的先序遍历、中序遍历、后序遍历)
		给定一个二叉树,返回所有从根节点到叶子节点的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 输入: 1 / \2 3 \ 5 输出: ["1->2->5", & ... 
- 【Leetcode】二叉树简单路径最大和问题
		问题一:二叉树任意两个叶子间简单路径最大和 示例: -100 / \ 2 100 / \ 10 20 思路:这个问题适用于递归思路. 首先,将问题简单化:假设包含最大和summax的简单 ... 
- [LeetCode] 257. Binary Tree Paths 二叉树路径
		Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ... 
- [LeetCode] Path Sum III 二叉树的路径和之三
		You are given a binary tree in which each node contains an integer value. Find the number of paths t ... 
- [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 ... 
- [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 ... 
- [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 ... 
随机推荐
- js工具库简单介绍
			javascript mvc的解决方案: angularjs, backbone,underscore, 有空的时候了解一下. 移动端的几个需要了解一下,jq mobile, zepto.knocko ... 
- ComicEnhancerPro 系列教程十七:二值化图像去毛刺
			作者:马健邮箱:stronghorse_mj@hotmail.com 主页:http://www.comicer.com/stronghorse/ 发布:2017.07.23 教程十七:二值化图像去毛 ... 
- C# 由范式编程==运算符引发对string内存分配的思考
			今天在看C#编程指南时(类型参数的约束http://msdn.microsoft.com/zh-cn/library/d5x73970.aspx)看到一段描述: 在应用 where T : class ... 
- web3部署智能合约碰到的一个奇怪问题
			都是gasLimit惹的祸 解决一个奇怪问题Error: Number can only safely store up to 53 bits 原来好好的node endpointtest.js ,结 ... 
- MVN package install error javac: invalid target release: 1.8
			现象:---------------------------------[ERROR] Failure executing javac, but could not parse the error:j ... 
- Launch VINS example (Euroc dataset) in RTAB-MAP
			$ roslaunch rtabmap_ros euroc_datasets.launch args:="-d RGBD/CreateOccupancyGrid false Odom/Str ... 
- React基础篇 (1)-- render&components
			render 基础用法 //1.创建虚拟DOM元素对象 var vDom=<h1>hello wold!</h1> //2.渲染 ReactDOM.render(vDom,do ... 
- win7系统电脑显示windows副本不是正版怎么办
			win7系统电脑显示windows副本,可以:在开始输入框中输入cmd——以管理员权限运行——在命令行中输入SLMGR -REARM,——重启. 
- SDUT OJ 数据结构上机测试1:顺序表的应用
			数据结构上机测试1:顺序表的应用 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ... 
- rest-assured的日志使用介绍
			在许多测试用例当中,为了帮助我们创建正确的断言和发送正确的请求,打印出详细的响应和请求数据是非常有用的.为此我们可以使用rest-assured提供的预定义过滤器或者使用其中的一些快捷方法. 一.请求 ... 
