【一天一道LeetCode】#257. Binary Tree Paths
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
1
/ \
2 3
\
5
All root-to-leaf paths are:
[“1->2->5”, “1->3”]
(二)解题
题目大意:给定一个二叉树,输出所有根节点到叶子节点的路径。
解题思路:采用深度优先搜索,碰到叶子节点就输出该条路径。
需要注意以下几点(也是我在解题过程中犯的错误):
- 需要考虑节点值为负数的情况,要转成string
- 要按照题目给定的格式来输出。
下面看具体代码:
/**
* 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> ret;
string tmp;
if(root!=NULL) dfsTreePaths(root,ret,tmp);
return ret;
}
void dfsTreePaths(TreeNode* root,vector<string>& ret, string tmp)
{
if(root->left == NULL&& root->right==NULL) {//如果为叶子节点就输出
char temp[10];
sprintf(temp, "%d", root->val);//将整数转换成string
tmp += string(temp);
ret.push_back(tmp);
return;
}
char temp[10];
sprintf(temp, "%d", root->val);//将整数转换成string
tmp += string(temp);
tmp +="->";
if(root->left !=NULL) dfsTreePaths(root->left,ret,tmp);//继续搜索左子树
if(root->right !=NULL) dfsTreePaths(root->right,ret,tmp);//继续搜索右子树
}
};
【一天一道LeetCode】#257. Binary Tree Paths的更多相关文章
- LeetCode 257. Binary Tree Paths (二叉树路径)
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- [LeetCode] 257. Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- Leetcode 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- (easy)LeetCode 257.Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- Java [Leetcode 257]Binary Tree Paths
题目描述: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tr ...
- [leetcode]257. Binary Tree Paths二叉树路径
Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...
- LeetCode 257. Binary Tree Paths(二叉树根到叶子的全部路径)
Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...
- Leetcode 257 Binary Tree Paths 二叉树 DFS
找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...
- <LeetCode OJ> 257. Binary Tree Paths
257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...
- 【LeetCode】257. Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
随机推荐
- poj3270 && poj 1026(置换问题)
| 1 2 3 4 5 6 | | 3 6 5 1 4 2 | 在一个置换下,x1->x2,x2->x3,...,xn->x1, 每一个置换都可以唯一的分解为若干个不交的循环 如上面 ...
- poj 1113 凸包周长
Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33888 Accepted: 11544 Descriptio ...
- HDU 1540 Tunnel Warfare(最长连续区间 基础)
校赛,还有什么途径可以申请加入ACM校队? Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/ ...
- 常用Markdown语法
个人常用的Markdown语法 前言 初次使用Markdown编辑器,将自己最常用的几个语法记录一下,如有错误或是更方便的使用方式还请指出. 多级标题 一个"#"到六个" ...
- Java连接FTP成功,但是上传是失败,报错:Connected time out
Java代码在本机上传文件到FTP服务器的时候成功,但是部署到测试服务器的时候出现,连接FTP成功但是上传失败,并且报Connected time out错误: 测试服务器和FTP服务都在阿里云上:( ...
- PTA 旅游规划(25 分)
7-10 旅游规划(25 分) 有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该公路要收取的过路费.现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径.如果有若干条 ...
- django 发送手机验证码
一.流程分析: 1.用户在项目前端,输入手机号,然后点击[获取验证码],将手机号发到post到后台. 2.后台验证手机号是否合法,是否已被占用,如果通过验证,则生成验证码,并通过运行脚本,让短信运营商 ...
- MySQL DATE_SUB()
DATE_SUB(date,INTERVAL expr type) 函数从日期减去指定的时间间隔. date 参数是合法的日期表达式.expr 参数是您希望添加的时间间隔. type 参数可以是下列值 ...
- bootmgr is missing 开机无法进系统怎么办
认识 bootmgr: 启动管理器.Bootmgr是Boot Manager的缩写,是在Windows Vista和Windows 7中使用的新的启动管理器,以代替Windows xp中的启动管理器- ...
- 读书笔记-《Maven实战》-2018/4/18
第五章:坐标和依赖 1.每个依赖中可以包含的元素有: groupId,artifactId,version: 这三个元素是Maven项目最重要的元素.Maven需要根据这三个坐标找到需要的依赖. ty ...