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. weblogic反序列化漏洞CVE-2018-2628-批量检测脚本

    #coding=utf-8 import socket import time import re,os,sys,codecs type = 'utf-8' reload(sys) sys.setde ...

  2. Ubuntu 安装 JDK8

    安装python-software-properties $sudo apt-get install python-software-properties $sudo apt-get install ...

  3. ajax方式提交表单数据并判断当前注册用户是否存在

    项目的目录结构 源代码: regservlet.java package register; import java.io.IOException; import java.io.PrintWrite ...

  4. css学习_css文字阴影、盒子阴影

    文字阴影和盒子阴影的用法: 多阴影

  5. sql数据库光标变成黑快怎么回事?

    可能是因为你按到了insert键啦,你再按一下insert键应该就可以啦. 光标变成块状说明当前是覆盖模式.光标变成竖条状说明当前是插入模式.

  6. git本地仓库 push到远程仓库出现错误

    ! [rejected] master -> master (fetch first) error: failed to push some refs to hint: Updates were ...

  7. Fiddler设置断点修改Request和Response

    一.Fiddler中修改Request有两种方法:  点击Rules-> Automatic Breakpoint ->Before Requset (这种方法会中断所有的会话) 消除命令 ...

  8. 认识Charles-proxy 抓包工具

    1.为什么不用 Fiddler 抓包工具? 在这里说明一下,因为Fiddler 抓包工具使用C#语言写的,不能在  MAC 上运行,而   Charles-proxy  他是 java  开发的,可以 ...

  9. 通过android studio的gradle强制cmake输出命令详情

    https://stackoverflow.com/questions/43439549/force-cmake-in-verbose-mode-via-gradle-and-the-android- ...

  10. Spark MLlib之使用Breeze操作矩阵向量

    在使用Breeze 库时,需要导入相关包: import breeze.linalg._ import breeze.numerics._ Breeze创建函数 //全0矩阵 DenseMatrix. ...