LeetCode OJ:Binary Tree Paths(二叉树路径)
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"]
简单的遍历查找路径问题,代码如下:
/**
* 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) {
ret.clear();
string s = "";
if(root == NULL) return ret;
dfs(root, s);
for(int i = ; i < ret.size(); ++i){
ret[i].erase(ret[i].begin(), ret[i].begin() + );
}
return ret;
} void dfs(TreeNode * root, string s)
{
stringstream ss;
ss << "->" << root->val;
s += ss.str();
if(root->left == NULL && root->right == NULL){
ret.push_back(s);
return;
}
if(root->left){
dfs(root->left, s);
}
if(root->right){
dfs(root->right, s);
}
}
private:
vector<string> ret;
};
java版本的如下所示,和c++的相比还是要简单很多的,因为处理字符串的函数用起来比较方便的原因,代码如下:
public class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> ret = new ArrayList<String>();
String str = new String();
if(root == null)
return ret;
dfs(root, str, ret);
return ret;
}
public void dfs(TreeNode root, String path, List<String> ret){
if(root.left != null){
path = path + "->" + root.val;
dfs(root.left, path, ret);
path = path.substring(0, path.lastIndexOf("->")); //引用其他的
} //还是要继续使用的,截断即可
if(root.right != null){
path = path + "->" + root.val;
dfs(root.right, path, ret);
path = path.substring(0, path.lastIndexOf("->"));
}
if(root.left == null && root.right == null){
path = path + "->" + root.val;
ret.add(path.substring(2));
path = path.substring(0,path.lastIndexOf("->"));
return;
}
}
}
LeetCode OJ: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. Note: A leaf is a node with no children. Example ...
- [LintCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 / \2 ...
- [LeetCode] 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 二叉树 DFS
找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...
- 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. Note: A leaf is a node with no children. Example ...
- 257 Binary Tree Paths 二叉树的所有路径
给定一个二叉树,返回从根节点到叶节点的所有路径.例如,给定以下二叉树: 1 / \2 3 \ 5所有根到叶路径是:["1->2->5", " ...
- 【easy】257. Binary Tree Paths 二叉树找到所有路径
http://blog.csdn.net/crazy1235/article/details/51474128 花样做二叉树的题……居然还是不会么…… /** * Definition for a b ...
- LeetCode(53)-Binary Tree Paths
题目: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree ...
随机推荐
- 印象笔记windows端-快捷键大全
作为印象笔记粉,当然要多掌握些快捷键,提高办公效率. 补: ctrl + shift + , 光标内字体变小 ctrl + shitf + . 光标内字体变大
- 0402-服务注册与发现-Eureka Server使用、将服务注册到Eureka server上
一.Eureka Server使用 官方文档地址:http://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud. ...
- 001-centos7安装 笔记本 联想G510
一.准备前提 1.联想G510AT 用winpe进入笔记本电脑,找到一个分区,删除即可 2.使用U盘安装 2.1.准备一个8G 的U盘,格式化ntfs. 2.2.在window下,下载UltraISO ...
- orange安装文档
一.Orange简介 Orange是一个基于 OpenResty/Nginx 的 API Gateway,提供 API 及 “自定义规则” 的监控和管理,如访问统计.流量切分.AB 测试.API ...
- jQuery Mobile panel的相关属性
参考网站:http://www.lampweb.org/jquerymobile/19/64.html 面板 data-role="panel" 在一个 jQuery Mobile ...
- django内容总结
一.django请求的生命周期 1.django请求生命周期如图所示 2.django本身没有socket,客户端请求先到达wsgi然后再提交给django,而wsgi的本质就是个socket程序 注 ...
- asp.net(c#)中String.Empty、NULL、"" 三者到底有啥区别和联系?
开门见山,首先看下面代码,你认为结果分别是什么? string str = string.Empty; string str1 = ""; string str2 = null; ...
- VC6.0中添加库文件和头文件
附加头文件包含 VC6.0中: VC6.0默认include包含路径:Tools>Options>Directories>Include files. 对于特定项目的头文件包含,在“ ...
- android 7.0 (nougat)的编译优化-ninja
http://blog.csdn.net/songjam/article/details/52640501 版权声明:本文为博主原创文章,未经博主允许不得转载. 从官方的定义,ninja大大缩短了an ...
- Jfreechart 生成不同数据源多个饼图(Multiple Pie Chart)
http://blog.163.com/ppy2790@126/blog/static/103242241201210130736274/ 项目中要用JfreeChart实现不同数据源多个饼图展现每个 ...