LeetCode 257 二叉树的所有路径
题目:
给定一个二叉树,返回所有从根节点到叶子节点的路径。
说明: 叶子节点是指没有子节点的节点。
示例:
输入: 1
/ \
2 3
\
5 输出: ["1->2->5", "1->3"] 解释: 所有根节点到叶子节点的路径为: 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) {
vector<string> ans;
if(root == NULL)
return ans;
if(!root->left && !root->right)
ans.push_back(to_string(root->val));
vector<string> leftSub = binaryTreePaths(root->left);
for(int i=; i<leftSub.size(); ++i)
ans.push_back(to_string(root->val) + "->" + leftSub[i]);
vector<string> rightSub = binaryTreePaths(root->right);
for(int i=; i<rightSub.size(); ++i)
ans.push_back(to_string(root->val) + "->" + rightSub[i]);
return ans;
}
};
参考来源:https://blog.csdn.net/my_clear_mind/article/details/82283939
法二:
/**
* 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:
void DFS(TreeNode* root, string temp, vector<string> &ans)
{
if(root == NULL) {
return ;
}
if(root->left == NULL && root->right == NULL) {
temp += to_string(root->val);
ans.push_back(temp);
return ;
}
if(root->left) {
DFS(root->left, temp + to_string(root->val) + "->", ans);
}
if(root->right) {
DFS(root->right, temp + to_string(root->val) + "->", ans);
}
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> ans;
if(root == NULL)
return ans;
DFS(root, "", ans);
return ans;
}
};
LeetCode 257 二叉树的所有路径的更多相关文章
- Java实现 LeetCode 257 二叉树的所有路径
257. 二叉树的所有路径 给定一个二叉树,返回所有从根节点到叶子节点的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 输入: 1 / \ 2 3 \ 5 输出: ["1->2 ...
- leetcode 257. 二叉树的所有路径 包含(二叉树的先序遍历、中序遍历、后序遍历)
给定一个二叉树,返回所有从根节点到叶子节点的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 输入: 1 / \2 3 \ 5 输出: ["1->2->5", & ...
- LeetCode 257.二叉树所有路径(C++)
给定一个二叉树,返回所有从根节点到叶子节点的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 输入: 1 / \ 2 3 \ 5 输出: ["1->2->5", ...
- 领扣(LeetCode)二叉树的所有路径 个人题解
给定一个二叉树,返回所有从根节点到叶子节点的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 输入: 1 / \ 2 3 \ 5 输出: ["1->2->5", ...
- LeetCode 124 二叉树中最大路径和
题目: 给定一个非空二叉树,返回其最大路径和. 本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列.该路径至少包含一个节点,且不一定经过根节点. 思路:递归 分为三部分,根节点,左子树,右 ...
- 【二叉树-所有路经系列(根->叶子)】二叉树的所有路径、路径总和 II、路径总和、求根到叶子节点数字之和(DFS)
总述 全部用DFS来做 重点一:参数的设置:为Root,路径字符串,路径List集合. 重点二:步骤: 1 节点为null 2 所有节点的操作 3 叶子结点的操作 4 非叶节点的操作 题目257. 二 ...
- [LeetCode] 257. Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- [LeetCode] 124. Binary Tree Maximum Path Sum 求二叉树的最大路径和
Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...
随机推荐
- 2017.11.18 手把手教你学51单片机-点亮LED
In Doing We Learning 在操作中学习.如果只是光看教程,没有实际的操作,对编程语言的理解很空泛,所以决定从单片机中学习C语言. #include<reg52.h> ...
- Technical poem
Apartment Good apartment be booked the second it bring to market. low product sold to many man, and ...
- 2019/4/11 wen 常用类2
- ant常用的内置 task转自https://www.cnblogs.com/baicj/archive/2015/12/21/5063608.html
ant 例如: <target name="callProjectB"> <echo message="In projectA calling proj ...
- C# 线程 正确使用Thread.Join()停止方式
/// <summary> /// 停下线程 /// </summary> private void MyStopTask() ...
- 马上AI全球挑战者大赛-违约用户风险预测
方案概述 近年来,互联网金融已经是当今社会上的一个金融发展趋势.在金融领域,无论是投资理财还是借贷放款,风险控制永远是业务的核心基础.对于消费金融来说,其主要服务对象的特点是:额度小.人群大.周期短, ...
- Learning-Python【1】:交互式环境与变量的使用
一.执行Python程序的两种方式 1. 交互式环境,打开cmd,输入python2或python3,显示提示符 “>>>”. 特点:输出代码立即执行 优点:调试程序方便 缺点:无法 ...
- JS对象、数据类型区别、函数
对象 基本数据类型都是单一的值,值和值之间没有任何联系,变量之间独立,不能成为一个整体. 对象属于一种符合的数据类型,对象中可以保存对个不同数据类型的属性. 对象分类: 1.内建对象 由ES标准 ...
- .Net文件压缩
NuGet中下载Ionic.Zip: public static class ZipHelper { /// <summary> /// 压缩文件 /// </summary> ...
- python多进程apply与apply_async的区别
为什么会这样呢? 因为进程的切换是操作系统来控制的,抢占式的切换模式. 我们首先运行的是主进程,cpu运行很快啊,这短短的几行代码,完全没有给操作系统进程切换的机会,主进程就运行完毕了,整个程序结束. ...