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. vue里的样式添加之类名改动 和style改动

    类名下有不同样式,通过增加或者减少类名,来增加或减少样式. v-bind:class = {类名:变量,类名:变量...}  变量为布尔值,如果是true则类名添加,反正类名不添加到该元素身上 v-b ...

  2. mui 卡片视图 遮罩蒙版

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

  3. java中的静态代理和动态代理

    1.动态代理的定义:为其他对象提供一个代理以控制对这个对象的访问 代理类主要负责委托类的预处理消息,过滤消息,把消息传给委托类以及消息事后处理 按照代理类的创建时期,代理类可以分为2种:静态代理类(在 ...

  4. 我了解到的新知识之--GDPR

    2018年5月25日GDPR正式实施,但是一直也是一知半解,今天偶然翻看到一篇企业撰写的关于GDPR的公众号文章,随即去网络上搜索了以下. 大家可以参考如下链接连接过于GDPR的细节,GDPR包括序言 ...

  5. Codeforces 1089E - Easy Chess - [DFS+特判][2018-2019 ICPC, NEERC, Northern Eurasia Finals Problem E]

    题目链接:https://codeforces.com/contest/1089/problem/E Elma is learning chess figures. She learned that ...

  6. 洛谷试炼场 - 关卡1-5 - 简单字符串 - (Done)

    P1055 ISBN号码 #include<bits/stdc++.h> using namespace std; string s; ]={','X'}; int main() { ci ...

  7. day19:常用模块(collections,time,random,os,sys)

    1,正则复习,re.S,这个在用的最多,re.M多行模式,这个主要改变^和$的行为,每一行都是新串开头,每个回车都是结尾.re.L 在Windows和linux里面对一些特殊字符有不一样的识别,re. ...

  8. nexus2 配置

    <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Soft ...

  9. 【Python全栈】HTML <!--...--> 注释 、CSS/JS //注释 和 /*.....*/ 注释

    HTML <!--...--> 注释 .CSS/JS //注释 和 /*.....*/ 注释 <!-- -->是HTML的注释标签,使用 < 和 > 是符合HTML ...

  10. 在ABP中使用linq

    private IQueryable<MembershipEntity> SelectOrScrrenMember(GetMemberInput input) { string[] use ...