题目:

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的更多相关文章

  1. LeetCode 257. Binary Tree Paths (二叉树路径)

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  2. [LeetCode] 257. Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  3. Leetcode 257. Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  4. (easy)LeetCode 257.Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  5. Java [Leetcode 257]Binary Tree Paths

    题目描述: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tr ...

  6. [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 ...

  7. 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 ...

  8. Leetcode 257 Binary Tree Paths 二叉树 DFS

    找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...

  9. 【LeetCode】257. Binary Tree Paths

    Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...

随机推荐

  1. 1090. Highest Price in Supply Chain (25) -计层的BFS改进

    题目如下: A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyon ...

  2. 1.QT中的容器QVector,QList,QSet,QMap,QQueue,QStack,QMultiMap,QSingleList等

    1  新建一个项目 在pro文件中只需要加上CONFIG += C++11 main.cpp #include <QMap> int main() { QMap<int,QStrin ...

  3. 【java集合框架源码剖析系列】java源码剖析之TreeMap

    注:博主java集合框架源码剖析系列的源码全部基于JDK1.8.0版本.本博客将从源码角度带领大家学习关于TreeMap的知识. 一TreeMap的定义: public class TreeMap&l ...

  4. SSH深度历险(十一) AOP原理及相关概念学习+xml配置实例(对比注解方式的优缺点)

    接上一篇 SSH深度历险(十) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP,本篇我们主要是来学习使用配置XML实现AOP 本文采用强制的CGLB代理方式 Security ...

  5. 压力测试工具Ab简介

    Apache安装包中自带的压力测试工具 Apache Benchmark(简称ab) 简单易用,这里就采用 ab作为压力测试工具了. 1.独立安装 通过 yum-utils中的yumdownload  ...

  6. ECMAScript 6之Set和Map数据结构

    Set 基本用法 ES6提供了新的数据结构Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set本身是一个构造函数,用来生成Set数据结构. var s = new Set(); [2, ...

  7. 记录github出错及解决方案

    刚刚在github上更新自己项目的一些内容时出现了一些错误,几经折腾及在网上查找资料终于解决.具体记录如下: 主要就是就是在push时报错,错误信息如下: 根据报错信息原本以为是要重新pull一下,但 ...

  8. Linux:alias永久生效

    alias(中文称为"别名")允许使用更加简短的名称来重新定义 Linux 中的 Shell 命令,从而简化命令行的输入. 如果经常与 CLI 打交道,那么使用 alias 不仅会 ...

  9. 《java入门第一季》之泛型方法和泛型接口

    一.泛型方法. /* * 泛型方法:把泛型定义在方法上.格式:public <泛型类型> 返回类型 方法名(泛型类型 t) public <T> void show(T t){ ...

  10. 【IOS 开发】Object-C 运算符

    博客地址 : http://blog.csdn.net/shulianghan/article/details/41624613 1. 算术运算符 算术运算符 : 加(+), 减(-), 乘(*), ...