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 ...
随机推荐
- android中SQLite实现
SQLite操作类: package com.example.administrator.myapplication; import android.content.Context; import a ...
- [bzoj3630][JLOI2014]镜面通道_计算几何_网络流_最小割
镜面通道 bzoj-3630 JLOI-2014 题目大意:题目链接. 注释:略. 想法: 我们发现,只要上下界没有被完全封死,我们就一定有一条合法的光路. 所以只需要将上界和下界拆开即可. 拆点,把 ...
- XJTUOJ13 (数论+FFT)
http://oj.xjtuacm.com/problem/13/ 题意:wmq如今开始学习乘法了!他为了训练自己的乘法计算能力,写出了n个整数, 并且对每两个数a,b都求出了它们的乘积a×b.现在他 ...
- Java日期LocalDate使用
在做报表统计时,需要对指定时间内的数据做统计,则需要使用到时间日期API 在此使用的是java.util.Date的完美私生子LocalDate类 LocalDate方法介绍 now() : 从默认时 ...
- IDUtil 永不重复的ID
package com.xxx.common.util; import java.util.Random; /** * 各种id生成策略 * * @version 1.0 */ public clas ...
- sqlit中使用到的查询语句
近期使用sqlite查询比較多,包含连表查询等. 记录一下.以免忘记! 1.先依据时间排序后选择前十条: select * from MyBill order by createTime desc ...
- 手把手教你编写一个简单的PHP模块形态的后门
看到Freebuf 小编发表的用这个隐藏于PHP模块中的rootkit,就能持久接管服务器文章,很感兴趣,苦无作者没留下PoC,自己研究一番,有了此文 0×00. 引言 PHP是一个非常流行的web ...
- BSON结构
BSON结构 flyfish 2015-7-24 主要解释bsonspec.org站点上的两个样例 {"hello": "world"} hello为key. ...
- android 特殊符号开头的联系人归并至“#”下
在PeopleActivity界面.联系人的显示位置是由其display name的第一个字符决定的. 数字开头的联系人会显示在"#"这个header下. 中英文联系人会显示在&q ...
- SDUST 2844-Mineral Water(数学)
Mineral Water nid=24#time" title="C.C++.go.haskell.lua.pascal Time Limit1000ms Memory Limi ...