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 ...
随机推荐
- Swift快速给Cocoa库内置类添加便捷初始化器
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) Cocoa中的NSShadow类默认没有我们需要的实例方法,为 ...
- 第一行代码阅读笔记----显示隐式Intent的基本用法
1.显示Intent意图明显,通过Intent启动另外一个活动,这是安卓中各组件进行交互的一种重要方式.一般用于启动活动,启动服务,发送广播等场景. 实现方法,这里我只说思路,实践还是要自己实操才能明 ...
- Android初级教程:对文件和字符串进行MD5加密工具类
转载请注明出处:http://blog.csdn.net/qq_32059827/article/details/52200008 点击打开链接 之前写过一篇博文,是针对字符串进行md5加密的.今 ...
- (一〇八)iPad开发之横竖屏适配
在iPad开发中,横竖屏的视图常常是不同的,例如侧边栏Dock,在横屏时用于屏幕较宽,可以展示足够多的内容,每个按钮都可以展示出标题:而竖屏时Dock应该比较窄,只显示图标不现实按钮标题. iPad比 ...
- 一台电脑上同启动两个Tomcat的方式,windows/Linux配置。
安装两个jdk,一个JDK路径在:C:\ProgramFiles (x86)\Java\jdk1.7.0_25,另外一个JDK的路径在E:\UCMSServer\j2sdk 在环境变量里并设置J ...
- 1. MariaDB简介
作者: 铁锚 日期: 2013年9月21日 官方博客地址:https://mariadb.org/ 官网地址: https://mariadb.com/ 百度百科地址: http://baike.ba ...
- Android官方技术文档翻译——Apk 拆分机制
本文译自androd官方技术文档<Apk Splits>,原文地址:http://tools.android.com/tech-docs/new-build-system/user-gui ...
- JSP连接access数据库
一个用jsp连接Access数据库的代码. 要正确的使用这段代码,你需要首先在Access数据库里创建一username表,表里面创建两个字符型的字段,字段名分别为:uid,pwd,然后插入几条测试数 ...
- java中,用json格式转换遇到问题
将list转为JSONObject类,报 org/apache/commons/lang/exception/NestableRuntimeException是什么原因? 还需要导入这些包common ...
- Android必知必会--使用shape制作drawable素材
前言 最近看到朋友制作的Android APP使用了极少的图片,但是图形却极其丰富,问了之后得知是使用shape绘制的,有很多优点. 下面是我整理的一些素材: 预览 下面是图片预览: 代码 布局文件 ...