【一天一道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 ...
随机推荐
- 2015 多校联赛 ——HDU5294(最短路,最小切割)
Tricks Device Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- [2017/5/28]FJ四校联考
来自FallDream的博客,未经允许,请勿转载,谢谢. 话说这一段时间算是过去了,好久好久之后终于又有联考了 没想到这次到我们学校出题,昨天才想起来,临时花一天赶了一套,我出了一个sbFFT,质量 ...
- bzoj2339[HNOI2011]卡农 dp+容斥
2339: [HNOI2011]卡农 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 842 Solved: 510[Submit][Status][ ...
- JavaScript和DOM
body { margin: 0 } .left { float: left } .right { float: right } .pg-head { height: 48px; background ...
- Python笔记(一)——打印输出
一.输出语句input 输出语句print 例:用户输入 username = input("username:") #变量名 显示的字符 password = input( ...
- jquery easyui datagrid改变某行的值
$("#DeterminateMembers").datagrid("updateRow",{index:index,row:{fzr:"0" ...
- html文本encode后,js获取参数失败的bug
html中的空格encodeURIComponent后变成%C2%A0,而js中的空格是'%20',二者无法匹配,所以要进行一次替换
- rhel7 配置普通用户使用sudo
rhel服务器版本安装之后,默认创建的用户不能使用sudo.使用sudo,会提示 user1 is not in the sudoers file. This incident will be rep ...
- Template Method 模板设计模式
什么是模板设计模式 对于不了解的模板设计模式的来说,可以认为如同古代的造纸术一样,纸所以成型,取决于用了模板的形状,形状又由镂空的木板组成,而你想要造什么纸,又取决于你使用什么材料. 上面提到了两个关 ...
- 转:函数signal()
from:http://blog.sina.com.cn/s/blog_4b226b92010119l5.html 当服务器close一个连接时,若client端接着发数据.根据TCP协议的规定,会收 ...