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 ...
随机推荐
- HTML、CSS、JavaScript网页制作从入门到精通 (刘西杰) pdf扫描版彩色版
html.css.JavaScript网页制作从入门到精通中从基础知识开始讲起,如html的基本标记.文字与段落标记.表格标记.超链接标记……同时介绍了目前流行的web标准与css网页布局实例,以及基 ...
- C#上位机中ZedGraph控件的使用
上位机程序控制PLC模拟量通道输出周期性正弦波信号,并采集所造成改变的模拟量输入信号,并绘制数据变化曲线. 界面如图: 最后测试效果如图: 代码: using System; using System ...
- C语言中无符号与有符号问题
unsigned char a[5] = { 12,36,96,128,182 }; a[]范围为0~256. 数组中数都有效. char a[5] = { 12,36,96,128,182 }; a ...
- angular 输出属性
import { Component, OnInit, EventEmitter, Output } from '@angular/core'; @Component({ selector: 'app ...
- android android各种应用的许可
android各种应用的许可 在Android的设计中,资源的访问或者网络连接,要得到这些服务都需要声明其访问权限,否则将无法正常工作.在Android中这样的权限有很多种,这里将各类访问权限一一罗列 ...
- javascript ie8兼容 a标签href javascript:void(0);
ie8兼容 a标签href javascript:void(0); 尽量不要用javascript:协议做为A的href属性,这样不仅会导致不必要的触发window.onbeforeunload事件;
- 第二篇:git创建流程
1.创建组织 2.创建 3.点击项目 创建完: 4.选择管理——>选择公钥——>添加个人公钥: 5.怎样生成公钥 5.1.如何生成ssh公钥 你可以按如下命令来生成 sshkey: ssh ...
- VS2013安装及破解教程
https://blog.csdn.net/qq_33742119/article/details/80075352 软件下载的百度云链接,也可以在官网直接下载 链接:https://pan.baid ...
- springboot集成巨杉数据库
springboot倾向于约定优于配置,所以大大简化了搭建项目的流程,包括各种数据源的配置,接下来就和大家分享下最近用到的巨杉数据源连接池的配置 1.现在配置文件中定义巨杉连接池的各种连接信息,至于每 ...
- MyBatis介绍及使用
一.介绍: 1.MyBatis实际上是Ibatis3.0版本以后的持久化层框架[也就是和数据库打交道的框架]! 2.和数据库打交道的技术有: 原生的JDBC技术--->Spring的JdbcTe ...