Given a binary tree, return all root-to-leaf paths.

Note: A leaf is a node with no children.

Example:

Input:

   1
/ \
2 3
\
5 Output: ["1->2->5", "1->3"] Explanation: All root-to-leaf paths are: 1->2->5, 1->3

题目

给定一棵二叉树,求出所有根到叶的路径。

思路

DFS, Very straightforward

代码

 class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> result = new ArrayList<>();
if(root == null) return result;
helper(root, result, "");
return result;
} private void helper (TreeNode root, List<String> result, String path){
// leaf node
if(root.left ==null && root.right ==null) {
result.add(path + root.val);
}
// dfs
if(root.left!=null){
helper(root.left, result, path+root.val+ "->");
}
if(root.right!=null){
helper(root.right, result, path+root.val+"->");
}
}
}

[leetcode]257. 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 二叉树 DFS

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

  3. [LintCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 /   \2 ...

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

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

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

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

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

  7. 257 Binary Tree Paths 二叉树的所有路径

    给定一个二叉树,返回从根节点到叶节点的所有路径.例如,给定以下二叉树:   1 /   \2     3 \  5所有根到叶路径是:["1->2->5", " ...

  8. 【easy】257. Binary Tree Paths 二叉树找到所有路径

    http://blog.csdn.net/crazy1235/article/details/51474128 花样做二叉树的题……居然还是不会么…… /** * Definition for a b ...

  9. Leetcode 257. Binary Tree Paths

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

随机推荐

  1. Java反射机制 —— 简单了解

    一.概述 JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为jav ...

  2. Java笔试基础01

    单例模式主要作用是保证在Java应用程序内,一个类只有一个实例存在. 手写单例 1.较为安全的写法 public class Singleton01{ private static Singleton ...

  3. EF中GroupBy扩展方法的简单使用

    public ActionResult ShopInfo() { ViewBag.ShopList = ShopService.GetEntities(x => x.IsDelete == fa ...

  4. CUDA C Programming Guide 在线教程学习笔记 Part 3

    ▶ 表面内存使用 ● 创建 cuda 数组时使用标志 cudaArraySurfaceLoadStore 来创建表面内存,可以用表面对象(surface object)或表面引用(surface re ...

  5. zabbix监控vCenter报错,无法自动发现主机

    公司机房停电检修,检修完成后重新上电,发现VCSA起不来了,尝试多次无法解决,无奈只好重装.重装VCSA 6.5U2之后又发现无法自动发现主机,报错如下: 'config.vpxd.stats.max ...

  6. quartz 定时任务的增删改

    参考:  https://blog.csdn.net/llmys/article/details/81069863

  7. 使用matplotlib 制图(柱状图、箱型图)

    柱状图: import pandas as pd import matplotlib.pyplot as plt data = pd.read_csv('D:\\myfiles\\study\\pyt ...

  8. FireDACQuery FDQuery New

    FDQuery FDQuery1->ChangeCount;也有UpdatesPending属性 FDQuery1->ApplyUpdates() ExecSQL('select * fr ...

  9. AS3 os与version 区别 使用Capabilities类获取Flash Player的信息

    AS3中flash.system.Capabilities类提供诸多静态的只读属性来描述应用程序当前所运行在的系统和运行时信息,如Flash Player,Adobe AIR,Flash Lite.通 ...

  10. HttpClient post封装

    /** * @title HttpUtils * @description post请求封装 * @author maohuidong * @date 2017-12-18 */ public sta ...