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 ...
随机推荐
- asp.net 中input radio checked 无效
把Jq代码中的$(...).attr("checked",true) 换成$(...).prop("checked",true) ,
- Sql Server 日期格式化函數 Convert
Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2015 10:57AM Select CONVERT(varchar(100), GETDATE( ...
- 内联函数背景、例子、与普通函数的区别及要注意的地方 ------新标准c++程序设计
背景: 使用函数能够避免将相同代码重些多次的烦恼,还能减少可执行程序的体积,但也会带来程序运行时间上的开销.函数调用在执行时,首先在栈中为形参和局部变量分配存储空间,然后还要将实参的值复制给形参,接下 ...
- 【转载】Java资源大全中文版
Java资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.awesome-java 就是 akullpp 发起维护的 Java 资源列表,内容 ...
- 使用Eclipse的几个必须掌握的快捷方式
“工若善其事,必先利其器”,感谢Eclipse,她 使我们阅读一个大工程的代码更加容易,在阅读的过程中,我发现掌握几个Eclipse的快捷键会使阅读体验更加流畅,写出来与诸君分享,欢迎补充. 1. C ...
- MongoSQL 复制数据表报错
报错内容为: [thread1] SyntaxError: identifier starts immediately after numeric literal @(shell):1:2 解决方案: ...
- Linux 常用命令大全(长期更新)
常见指令 打包压缩相关命令 关机/重启机器 Linux管道 vim使用 用户及用户组管理 文件权限管理 更改文件的用户及用户组 更改权限 常用指令 ls 显示文件或目录 -l 列出文件详细信息l(li ...
- ajax返回数据成功 却进入error方法
应该是dataType的原因,dataType为json,但是返回的data不是json格式 于是将dataType:"json"去掉就ok了
- phpstorm利用database连接mysql数据库
首先声明一点,database只能连接一个已存在的数据库,不能创建数据库 连接一个已存在的数据库步骤: 1,找到database:连续点击俩次shift,输入database就能找到了 2,点击绿色的 ...
- 【转】IDEA快捷键功能说明及Eclipse对应操作
1.Ctrl+z是撤销快捷键 2.如果想恢复Ctrl+z 掉的内容,按快捷键为:Ctrl + Shift + Z.方可 3.Ctrl-H(Browse Type Hierarchy) Ctrl + A ...