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 ...
随机推荐
- memcpy用法
函数原型 void *memcpy(void*dest, const void *src, size_t n); 功能 由src指向地址为起始地址的连续n个字节的数据复制到以destin指向地址为起始 ...
- JAVA自动装箱拆箱与常量池
java 自动装箱与拆箱 这个是jdk1.5以后才引入的新的内容,作为秉承发表是最好的记忆,毅然决定还是用一篇博客来代替我的记忆: java语言规范中说道:在许多情况下包装与解包装是由编译器自行完成的 ...
- openstack组件服务的入口寻找方法
在centos7系统上,安装openstack服务以后,可以通过以下命令,查找到该系统上,已经安装的openstack服务 [root@xzto01n010027244133 ~]# systemct ...
- 《javascript 高级程序设计》 笔记1 1~7章
chapter 2 在html中使用JavaScript chapter 3 基本概念 EMCAscript 语法 变量,函数名和操作符都区分大小写. 使用var定义的变量将成为定义该变量的作用域中的 ...
- 浅谈Spring的两种配置容器
浅谈Spring的两种配置容器 原文:https://www.jb51.net/article/126295.htm 更新时间:2017年10月20日 08:44:41 作者:黄小鱼ZZZ ...
- 网页控件很多时最好用绝对定位absolute
否则动一个位置可能影响其他的,牵一发而动全身
- P3705 [SDOI2017]新生舞会 01分数规划+费用流
$ \color{#0066ff}{ 题目描述 }$ 学校组织了一次新生舞会,Cathy作为经验丰富的老学姐,负责为同学们安排舞伴. 有\(n\)个男生和\(n\)个女生参加舞会买一个男生和一个女生一 ...
- wpa_supplicant
一 函数接口介绍 wpa_ctrl_open接口用来打开wpa_supplicant的控制接口,在UNIX系统里使用UNIX domain sockets,而在Windows里则是使用UDP sock ...
- socket长连接 GCDAsyncSocket
基础: http://www.2cto.com/kf/201609/546974.html 转自: http://blog.csdn.net/u013282507/article/details/52 ...
- JUC包下CountDownLatch学习笔记
CountDownLatch的作用是能使用多个线程进来之后,且线程任务执行完毕之后,才执行, 闭锁(Latch):一种同步方法,可以延迟线程的进度直到线程到达某个终点状态.通俗的讲就是,一个闭锁相当于 ...