LeetCode(53)-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"]
思路:
- 题意:求二叉树根到叶子的所有路径
- 二叉树的问题,没有一次递归解决不了的,如果有,那就两次,判断左右子树,都为空是叶子节点,否则一直递归下去,然后传入List参数和ArrayList参数
-
代码:
public class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> all = new ArrayList<String>();
if(root == null){
return all;
}
getTreePaths(all,new ArrayList<Integer>(),root);
return all;
}
private void getTreePaths(List<String> result,List<Integer> tmp,TreeNode node){
if(node == null){
return;
}else{
tmp.add(node.val);
}
if(node.left == null && node.right == null){
result.add(getString(tmp));
}
if(node.left != null){
getTreePaths(result,new ArrayList(tmp),node.left);
}
if(node.right != null){
getTreePaths(result,new ArrayList(tmp),node.right);
}
}
private String getString(List<Integer> tmp){
StringBuffer sb= new StringBuffer();
if(tmp == null||tmp.isEmpty()){
return null;
}
sb.append(tmp.get(0));
for(int i = 1;i < tmp.size();i++){
sb.append("->").append(tmp.get(i));
}
return sb.toString();
}
}
LeetCode(53)-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. For example, given the following binary tree: 1 ...
- Leetcode 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- (easy)LeetCode 257.Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- Java [Leetcode 257]Binary Tree Paths
题目描述: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tr ...
- [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. Note: A leaf is a node with no children. Example ...
- Leetcode 257 Binary Tree Paths 二叉树 DFS
找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...
- 【LeetCode】257. Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
随机推荐
- FFmpeg源代码简单分析:libswscale的sws_getContext()
===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...
- 08 ListView 优化
ListVie的优化 1 固定ListView长宽高 如下图在清单文件中: <ListView android:id="@+id/lv" android:layout_wid ...
- SimpleAdapter和Baseadapter填充listActivity-android学习之旅()
简介 SimpleAdapter的功能是能够为AbsListView提供复杂的数据,需要构造ListView 代码示例 package peng.liu.testview; import androi ...
- OpenCV3.0 3.1版本的改进
摘要 OpenCV现在更新到了3.1版本,相对OpenCV2有了很大改进,其中对于硬件加速,移动开发(IOS,android)的支持成为亮点. 新版的OpenCV采用了内 ...
- OC可点击的两种轮播图效果
基本上,每一个APP都有一个轮播图的效果展示,一般都是用来展示图片的一些信息,然后可以点击查看或购买,所以在此我将这种轮播图进行了一个类的封装,效果包含两种形式:第一种,来回轮转样式,第二种,一个方向 ...
- java方法重写和super关键字
//java方法重写和super关键字 //在继承中,其实就是子类定义了和父类同名的方法 //就是方法,属性都是相通的 //重写限制: //被子类重写的方法不能拥有比父类方法更加严格的权限 //sup ...
- Uva - 1598 - Exchange
本来想用优先队列做,可是不知道怎么处理之间的关系,最后还是用了map方法AC了,不过速度上有些慢,提交的时候跑了1.557秒.估计这道题时间都稍微长些,题目的时间限制也是4.5秒,不像一般题目的3秒限 ...
- 【翻译】在Ext JS 6通用应用程序中使用既共享又特定于视图的代码
原文:Using Both Shared and View-Specific Code in an Ext JS 6 Universal App 在本文,在展示如何编写Ext JS 6通用应用程序代码 ...
- Java Web 高性能开发,第 3 部分: 网站优化实战
这个系列的前两篇,介绍了前端的优化技术,这些技术秉承了前人至高无上的智慧,我只是负责吸收和传播.然而,这些技术一般也都是某某大型网站的技术经验,我们大部分人或许只能接触到相对小规模的网站,小规模的网站 ...
- Ubuntu sudo 免密码之 sudoers 修改
Ubuntu sudo 免密码之 sudoers 修改 重要提示: 本文内容仅在虚拟机上实验通过.如果你不确信这个过程,请不要擅自改变/etc/sudoers.否则可能导致你的机器不可用.本文对由此产 ...