257 Binary Tree Paths 二叉树的所有路径
给定一个二叉树,返回从根节点到叶节点的所有路径。
例如,给定以下二叉树:
1
/ \
2 3
\
5
所有根到叶路径是:
["1->2->5", "1->3"]
详见:https://leetcode.com/problems/binary-tree-paths/description/
Java实现:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> res=new ArrayList<String>();
if(root==null){
return res;
}
helper(root,"",res);
return res;
}
private void helper(TreeNode root,String out,List<String> res){
out+=String.valueOf(root.val);
if(root.left==null&&root.right==null){
res.add(out);
}
if(root.left!=null){
helper(root.left,out+"->",res);
}
if(root.right!=null){
helper(root.right,out+"->",res);
}
}
}
C++实现:
/**
* 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> res;
if(root)
{
helper(root,"",res);
}
return res;
}
void helper(TreeNode *root,string out,vector<string> &res)
{
out+=to_string(root->val);
if(!root->left&&!root->right)
{
res.push_back(out);
}
if(root->left)
{
helper(root->left,out+"->",res);
}
if(root->right)
{
helper(root->right,out+"->",res);
}
}
};
参考:https://www.cnblogs.com/grandyang/p/4738031.html
257 Binary Tree Paths 二叉树的所有路径的更多相关文章
- 【easy】257. Binary Tree Paths 二叉树找到所有路径
http://blog.csdn.net/crazy1235/article/details/51474128 花样做二叉树的题……居然还是不会么…… /** * Definition for a b ...
- [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. 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 OJ> 257. Binary Tree Paths
257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...
- [LintCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 / \2 ...
- 【LeetCode】257. Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
- 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 ...
随机推荐
- 恢复表数据的办法(delete删除可恢复,truncate不可恢复)
select * from table_name as of timestamp to_timestamp('2018-12-20 00:00:00', 'yyyy-mm-dd hh24:mi:ss' ...
- 洛谷 P4470 [BJWC2018]售票
P4470 [BJWC2018]售票 C 市火车站最近出现了一种新式自动售票机.买票时,乘客要先在售票机上输入终点名称.一共有N 处:目的地,随着乘客按顺序输入终点名称的每个字母,候选终点站数目会逐渐 ...
- Linux下使用make install安装的软件如何卸载
如果是Ubuntu的系统,那么可以使用checkinstall来生成deb包来安装,然后卸载 参考:http://blog.sina.com.cn/s/blog_4178f4bf0101cmt7.ht ...
- 消息队列RabbitMQ使用教程收集
学习应该要系统,最好的方式是看书. RabbitMQ最权威的教程应该参考官方文档. 下面是收集的一些教程: 官方: https://www.rabbitmq.com/getstarted.html h ...
- mybatis resultmap标签type属性什么意思
mybatis resultmap标签type属性什么意思? :就表示被转换的对象啊,被转换成object的类型啊 <resultMap id="BaseResultMap" ...
- POJ 3370 Halloween treats(抽屉原理)
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6631 Accepted: 2448 ...
- cocos2d-x+lua开发模式下编辑器的选择
原本打算直接用CocosIDE的,毕竟是官方出品,并且支持Android远程调试,windows下的调试也非常方便,调试的信息也非常全,智能提示也不错.好了,一切看上去非常完美,可是它有一个致命缺陷, ...
- 两种常见的UITabBarController+UINavigationController模式分析比较
绝大部分软件都采用了UITabBarController+UINavigationController的设计模式,这是一种很主流很经典的设计方式,而另外一种UINavigationController ...
- Qt 插件综合编程-基于插件的OpenStreetMap瓦片查看器client(5) 小结
经过不断试用与改动,这个查看器终于还是完毕了设计.实现.查看器,顾名思义,没有编辑功能:说的白一点,仅仅是一个以OpenStreetMap为底图的显示装置罢了.和专业GIS相比,这款基于插件的Open ...
- Android清单文件具体解释(四) ---- backupAgent的使用方法
在<application>节点中有一个很重要的属性,那就是backupAgent.这里我们将它单独列出来,从基本含义,使用方法及其相关属性等方面来具体介绍一下. 1.backupAgen ...