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. 如何下载spring的jar包?

    文转载至:https://www.cnblogs.com/yjmyzz/p/3847364.html Spring官网改版后,很多项目的完整zip包下载链接已经隐掉了,虽然Spring旨在引导大家用更 ...

  2. IP地址与子网掩码

    IP地址 众所周知,为了确保通信时能相互识别,在internet上的每台主机都必须有一个唯一的标识,即主机的IP地址.IP协议就是根据IP地址来实现信息传递的. IP地址由32位(4字节)二进制数组成 ...

  3. CRM 2016 级联过滤 类比省市县

    以下以省市为例: function preFilterLookup() { //要进行过滤的lookup按钮加入addPresearch事件 Xrm.Page.getControl("shi ...

  4. Vue 封装js

    //封装模块化文件 新建的.js文件 var storage = { set(key, value) { localStorage.setItem(key, JSON.stringify(value) ...

  5. python 字符串与字节之间的相互转化

    1.将字符串转化成字节 b'fffff' bytes('ffff', encoding='utf-8') 'ffff'.encode('utf-8') 2.将字节转化成字符串 str(data, en ...

  6. HTML5 Canvas ( 事件交互, 点击事件为例 ) isPointInPath

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. leetcode138

    /** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...

  8. PHP依赖注入

    对于依赖注入 我现在的理解是把一个方法当成一个变量放进另一个方法的形参里 <?php class Factory { public static function getDb(){ return ...

  9. Eclipse 中yml自动提示功能相关设置

    转自:https://blog.csdn.net/lililuni/article/details/82849376

  10. awk分割字符串

    想从目标字符串中,提取想要的字符,可以用awk命令. 例如: 从<version>1.3.1-SNAPSHOT</version>中提取版本号,则可以用命令:awk -F'[& ...