480. Binary Tree Paths

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

Example

Example 1:

Input:

   1
/ \
2 3
\
5 Output: [
"1->2->5",
"1->3"
]

Example 2:

Input:

   1
/
2 Output: [
"1->2"
]

注意:

因为题目的output格式 "1 -> 2",(有箭头),所以用String来存储每一个path。

递归法代码:

/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/ public class Solution {
/**
* @param root: the root of the binary tree
* @return: all root-to-leaf paths
*/
ArrayList<String> result; public List<String> binaryTreePaths(TreeNode root) {
result = new ArrayList<String>();
if (root == null) {
return result;
} String path = String.valueOf(root.val);
helper(root, path);
return result;
}
public void helper(TreeNode root, String path) { if (root.left == null && root.right == null) {
result.add(path);
return;
} if (root.left != null) {
helper(root.left, path + "->" + String.valueOf(root.left.val));
} if (root.right != null) {
helper(root.right, path + "->" + String.valueOf(root.right.val));
}
}
}

Leetcode480-Binary Tree Paths-Easy的更多相关文章

  1. LeetCode_257. Binary Tree Paths

    257. Binary Tree Paths Easy Given a binary tree, return all root-to-leaf paths. Note: A leaf is a no ...

  2. &lt;LeetCode OJ&gt; 257. Binary Tree Paths

    257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...

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

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

  4. LintCode Binary Tree Paths

    Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Given the following binary tre ...

  5. 【LeetCode】257. Binary Tree Paths

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

  6. 606. Construct String from Binary Tree 【easy】

    606. Construct String from Binary Tree [easy] You need to construct a string consists of parenthesis ...

  7. (easy)LeetCode 257.Binary Tree Paths

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

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

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

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

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

  10. leetcode : Binary Tree Paths

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

随机推荐

  1. Oracle课程档案,第九天

    lsnrctl status:查看监听状态 Oracle网络配置三部分组成:客户端,监听,数据库 配置文件:$ vi $ORACLE_HOME/network/admin/listener.ora v ...

  2. angularjs实例

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script sr ...

  3. [bash][awk] bash下使用awk方便的列求和

    这么多年,始终在用awk进行文本处理.但是一直没有好好的学习awk的语法.所以很多情况都是知其然,不知其所以然. 如今,亦如此.先记下来如下,以后有时间系统的学习一下awk的语法. ┬─[tong@T ...

  4. Linux 从源码编译安装 OpenSSH

    https://blog.csdn.net/bytxl/article/details/46639073 Linux 从源码编译安装 OpenSSH以及各问题解决 2015年06月25日 17:37: ...

  5. 亿图图示 Edraw Max v9.2 完美破解版

    主程序:http://www.edrawsoft.cn/2download/edrawmax-cn-9.2.exe破解补丁:https://www.lanzous.com/i1fjsyh 密码:52p ...

  6. Elasticsearch学习笔记(三)聚合分析Agg

    一.设置fielddata PUT /index/_mapping/type {     "properties":{          "fieldName" ...

  7. typescript 如何引入jquery

    webpack配置,不需要配置externals,webpack具体配置如下, const webpack = require('webpack'); const path = require('pa ...

  8. .net core使用ef core操作mysql数据库

    新建.net core webapi项目 在NuGet包管理器中搜索 MySql.Data.EntityFrameworkCore并安装,安装的8.0.14版本,只安装这一个就够了 安装后创建Data ...

  9. 002-红黑树【B-树】、二叉查找树

    一.引述-二叉查找树 红黑树(Red Black Tree) 一种特殊的二叉查找树.故先查看二叉查找树 二叉查找树特性:左字数上所有的节点的值都小于或等于他的根节点上的值 右子树上所有节点的值均大于或 ...

  10. Mac下搭建solr搜索引擎与PHP扩展开发(上)

    首先需要安装jdk,前往 https://www.oracle.com/technetwork/java/javase/downloads/jdk12-downloads-5295953.html 自 ...