题目:

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. java虚拟机 jvm 栈数据区

    java栈帧还是需要一些数据支持常量池的解析.正常方法的返回和异常的处理.大部分的java字节码指令需要进行常量池的访问,在栈帧数据区中保存着访问常量池的指针,方便程序访问java常量池.如下图所示: ...

  2. 百度地图隐藏缩放控件比例尺Logo

    对于百度地图最新版V3.7.3,以前的隐藏控件方法失效,可用以下方法隐藏: 1.隐藏缩放控件: mMapView.showZoomControls(false); 2.隐藏比例尺: mMapView. ...

  3. 07 设置View的显示与隐藏

    在代码中: 例子: <span style="font-size:14px;">ImageButton imageButton = new ImageButton(th ...

  4. Handler,MessageQueue Loop 和Message的原理解析

    先介绍和handler一起工作的几个组件 Handler的方法介绍 代码示例 package liu.peng.weather; import java.util.Timer; import java ...

  5. listview下拉刷新上拉加载扩展(三)-仿最新版美团外卖

    本篇是基于上篇listview下拉刷新上拉加载扩展(二)-仿美团外卖改造而来,主要调整了headview的布局,并加了两个背景动画,看似高大上,其实很简单: as源码地址:http://downloa ...

  6. UNIX网络编程——套接字选项(setsockopt)

    setsockopt的一些用法: close socket(一般不会立即关闭而经历TIME_WAIT的过程)后想继续重用该socket: BOOL bReuseaddr=TRUE; setsockop ...

  7. UNIX环境高级编程——主线程与子线程的退出关系

    我们在一个线程中经常会创建另外的新线程,如果主线程退出,会不会影响它所创建的新线程呢?下面就来讨论一下. 1.  主线程等待新线程先结束退出,主线程后退出.正常执行. 示例代码: #include & ...

  8. 动态游标(例如表名作为参数)以及动态SQL分析

    表名作为参数的动态游标 DECLARE v_table_name VARCHAR2(30) := 'CUX_MES_WIP_BARCODE_MAP'; --l_rec SYS_REFCURSOR; T ...

  9. 设计模式之——工厂模式(A)

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41085085 昨天看完了工厂模式,觉得在开发的过程中好多地 ...

  10. [asp.net]登录协同工作平台安全解决方案

    [摘要]公司领导说登录验证的安全性如何保证,建议采用UKEY验证类似网银解决,调用第三方YT公司产品. 解决方案: 前端页面: <embed id="s_simnew61" ...