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 ...
随机推荐
- 51nod 1298 圆与三角形 (计算几何)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1298 求出圆心到三条线段的最短距离,然后判断是否有顶点在圆外,就把全部情 ...
- Ubuntu 16.04安装ntopng流量监控软件
ntop官方在2012年就已经不再更新,取代的是ntopng.ntopng具有Web页面,适合做网络管理监控软件.比如监控局域网内多台机器的上网情况等. 不过这个东西感觉不太准,最好的方案应该把安装n ...
- Oldboy 基于Linux的C/C++自动化开发---MYSQL
http://www.eimhe.com/forum.php?mod=viewthread&tid=142952#lastpost http://www.eimhe.com/thread-14 ...
- GNS3模拟的硬件
Hardware emulated by GNS3 Cisco 1700 Series 1700s have one or more interfaces on the motherboard, 2 ...
- ArcGIS for Android入门程序之DrawTool2.0
来自:http://blog.csdn.net/arcgis_mobile/article/details/8084763 GISpace博客<ArcGIS for Android入门程序之Dr ...
- Android 实现形态各异的双向側滑菜单 自己定义控件来袭
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/39670935.本文出自:[张鸿洋的博客] 1.概述 关于自己定义控件側滑已经写了 ...
- Linux学习日志--文件搜索命令
开头总结: 学习了Linux中的文件搜索命令find和locate,系统搜索命令whereis 和which ,字符串搜索命令grep,find和locate的差别和使用方法格式,什么是path环境变 ...
- [WebGL入门]五,矩阵的基础知识
注:文章译自http://wgld.org/,原作者杉本雅広(doxas),文章中假设有我的额外说明.我会加上[lufy:],另外.鄙人webgl研究还不够深入,一些专业词语,假设翻译有误,欢迎大家指 ...
- GSON学习笔记之初识GSON
引用"JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,採用全然独立于语言的文本格式.为Web应用开发提供了一种理想的数据交换格式. " ...
- linux 线程同步(二)
信号量 信号量是相互排斥锁的升级版把相互排斥锁中1变成了n.举个简单的样例:如果如今有10个人,有一部手机.这10个人都竞争来使用手机打电话这就是相互排斥锁.对于信号量,如今可能是有4部手机,这10个 ...