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 ...
随机推荐
- GC垃圾回收算法
什么是GC垃圾回收呢.日常生活中我们去餐厅吃饭吃完饭,吃完饭走了餐具不用管,服务员在把餐具拿走,这是一种方式,服务员怎么知道他要来把餐具拿走呢,因为你走了,这个位置空了.服务员什么时候拿走餐具很重要, ...
- 14 fragment传值
两个fragment传值 方式一 布局文件代码: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/and ...
- antlr v4 使用指南连载2——准备环境
antlr v4 开发环境 从上一篇文章的例子中可以知道,antlr有一套自己的语法来声明目标语言的语法,因此它本身就需要编译或者使用antlr提供的api来读取这些语法规则,并使之可 ...
- 如何查看Android设备上的分区信息
Android设备上,一般都会存在一块eMMC存储芯片来存放系统和用户数据,甚至部分的引导程序. 一般设备出厂时,各个厂商都会将这块存储芯片分成很多的分区,每个分区内存放不同的内容.具体分区的布局每个 ...
- FFmpeg示例程序合集-Git批量获取脚本
此前做了一系列有关FFmpeg的示例程序,组成了<FFmpeg示例程序合集>,其中包含了如下项目:simplest ffmpeg player: 最简单的 ...
- 音乐API
博主在前几篇博客中介绍了小Q聊天机器人的源码及其包含的一些功能,并在应用市场上上线了一个版本,其中有一个功能是歌曲搜索,即输入格式为"歌曲#歌曲名#歌手"即可搜索出相应的歌曲并进行 ...
- 【java集合框架源码剖析系列】java源码剖析之LinkedList
注:博主java集合框架源码剖析系列的源码全部基于JDK1.8.0版本. 在实际项目中LinkedList也是使用频率非常高的一种集合,本博客将从源码角度带领大家学习关于LinkedList的知识. ...
- (一〇四)使用Xcode6创建framework动态静态库
在Xcode6以前,创建framework可以使用iOS-Universal-Framework模板来创建framework,现在苹果已经提供了模板,如下图选择: 使用此模版创建的默认是动态库,方法和 ...
- (NO.00004)iOS实现打砖块游戏(九):游戏中小球与反弹棒的碰撞
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 前一篇博文介绍了物理对象中小球与砖块的碰撞处理,在这一篇中我们再 ...
- 学习Tensorflow,使用源码安装
PC上装好Ubuntu系统,我们一步一步来讲解如何使用源码安装tensorflow?(我的Ubuntu系统是15.10) 安装cuda 根据你的系统型号选择相应的cuda版本下载 https://de ...