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. 记一次maven的包冲突经历

    上周工作遇到一个特别棘手的bug,花了我一天时间去搞. 事情是这样的,打包那边的同事过来跟我说我的项目无法运行自动打包,卡在maven package上面,报错为:[error]未经检查的异常,需要捕 ...

  2. Windows下64位Apache+PHP+MySQL配置

    软件下载 目前,Apache和PHP均未出现官方的64位版本. Apache 64位: http://files.cnblogs.com/liangjie/httpd-2.2.19-win64.rar ...

  3. bootstrap-treeview 中文开发手册

    官方文档URL:  https://www.npmjs.com/package/bootstrap-treeview 2017年11月21日10:45:10 演示:http://www.htmleaf ...

  4. 转 docker创建私有仓库和k8s中使用私有镜像

    docker私有仓库建立 环境说明我们选取192.168.5.2做私有仓库地址yum install docker -y1.启动docker仓库端口服务 docker run -d -p 5000:5 ...

  5. C# 让String.Contains忽略大小写

    在C#里,String.Contains是大小写敏感的,所以如果要在C#里用String.Contains来判断一个string里是否包含一个某个关键字keyword,需要把这个string和这个ke ...

  6. ADG日志传输方式参数log_archive_dest_n详解

    主库的日志发送是由log_archive_dest_n参数设置(注意:同时还有一个和它相对应的开关参数log_archive_dest_state_n,用于指定该参数是否有效),下面简单介绍下该参数各 ...

  7. Linux学习8-CentOS部署自己本地的django项目

    前言 自己本地写好的django项目,如何部署到linux服务器上,让其他的小伙伴也能访问呢?本篇以centos系统为例,把本地写好的django项目部署到linux服务器上 环境准备: 环境准备: ...

  8. QSS独门秘籍:subcontrol

    QSS是C++ Qt中的界面美化神器,其语法和CSS区别不大,但是QSS有一个独有的功能——subcontrol,这是CSS所没有的,一个widget往往由多个子部件构成,利用subcontrol可以 ...

  9. ionic3.x版本-实现点击tab导航栏判断是否已经登陆然后加载不同页面,和退出登录功能。

    html代码: <ion-tabs #myTabs> <ion-tab [root]="tab1Root" tabTitle="首页" tab ...

  10. android从IIS/asp.net下载apk文件

    解决步骤: 1.web.config中 <configuration>  <configSections> ...    <section name="rewr ...