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 ...
随机推荐
- QT-Embedded-4.5.3在海思35xx上移植
QT4.5.3在海思3520A上移植步骤-修订版 2015年3月29日星期日, 16:59:03 1.首先要保证已经安装了海思的交叉编译器: #arm-hi + Tab key to show wh ...
- Servlet开发(2)
Jsp&Servlet用户登录功能实现(采用MVC模式) 我们使用Jsp&Servlet开发一个用户登录功能的小项目(麻雀大小,但是五脏俱全呦,关键是技术问题!). 数据库:mysql ...
- java 8种基本类型与对应的包装类
数据类型 包装类 字节长度 默认值 有效位 byte Byte 1 0 -128~127 short Short 2 0 -32768~32767 int Integer 4 0 -2^31-1~2^ ...
- Android GIS开发系列-- 入门季(8) Json与Geometry的相互转换
在Android中json数据十分普遍,也很实用,在Arcgis中也同样支持Json数据,Json与Geometry可以相互转换,达到我们想要的数据. 一.Geometry转换成Json数据 这个实现 ...
- SaltStack学习笔记之安装zabbix_agentd(jinja和pillar)
一.环境说明 机器 IP 主机名 Master 192.168.0.23 minion.saltstack.com Minion 192.168.0.35 minion-node2.saltstack ...
- 实战c++中的vector系列--emplace_back造成的引用失效
上篇将了对于struct或是class为何emplace_back要优越于push_back,可是另一些细节没有提及.今天就谈一谈emplace_back造成的引用失效. 直接撸代码了: #inclu ...
- quick-cocos2d-x游戏开发【1】——引擎结构总览和创建项目
好吧,我还是忍不住想写点关于quick的学习笔记,确实网上关于它的教程太少太少了,简单把自己的所学所得分享一下,有不正确之处还请拍砖. 首先下载引擎包.触控收购quick之后.如今cocos中文站的主 ...
- SQLite数据库基本操作
SQLite 是一个开源的嵌入式关系数据库,实现自包容.零配置.支持事务的SQL数据库引擎. 其特点是高度便携.使用方便.结构紧凑.高效.可靠. 与其他数据库管理系统不同,SQLite 的安装和运行非 ...
- ajax 跨域查看
var CSRF_HEADER = 'X-CSRF-Token'; var setCSRFToken = function(securityToken) { jQuery.ajaxPrefilter( ...
- Netty In Action中文版 - 第四章:Transports(传输)
本章内容 Transports(传输) NIO(non-blocking IO,New IO), OIO(Old IO,blocking IO), Local(本地), Embedded(嵌入式) U ...