Leetcode480-Binary Tree Paths-Easy
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的更多相关文章
- 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 ...
- <LeetCode OJ> 257. Binary Tree Paths
257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...
- [LintCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 / \2 ...
- LintCode Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Given the following binary tre ...
- 【LeetCode】257. Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
- 606. Construct String from Binary Tree 【easy】
606. Construct String from Binary Tree [easy] You need to construct a string consists of parenthesis ...
- (easy)LeetCode 257.Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- 【easy】257. Binary Tree Paths 二叉树找到所有路径
http://blog.csdn.net/crazy1235/article/details/51474128 花样做二叉树的题……居然还是不会么…… /** * Definition for a b ...
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- leetcode : Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
随机推荐
- 【漏洞复现】Tomcat CVE-2017-12615 远程代码执行漏洞
漏洞描述 [漏洞预警]Tomcat CVE-2017-12615远程代码执行漏洞/CVE-2017-12616信息泄漏 https://www.secfree.com/article-395.html ...
- js运用5
js数据类型具体分析 1.基础类型:string number boolean null undefined 2.引用类型:object==> json array ...
- Codeforces 1136E - Nastya Hasn't Written a Legend - [线段树+二分]
题目链接:https://codeforces.com/problemset/problem/1136/E 题意: 给出一个 $a[1 \sim n]$,以及一个 $k[1 \sim (n-1)]$, ...
- laravel使用记录
引用外部文件方式参考地址:https://blog.csdn.net/Darry_Zhao/article/details/52689635 跟踪数据库执行语句 DB::enableQueryLog( ...
- 数据类型&字符串得索引及切片
一:数据类型 1):int 1,2,3用于计算 2):bool ture false 用于判断,也可做为if的条件 3):str 用引号引起来的都是str 存储少量数据,进行 ...
- jQuery 学习笔记(3)(内容选择器、attr方法、prop方法,类的操作)
内容选择器: 1.$("div:empty"): 空的div元素 2.$("div:parent"): 非空div元素 3.$("div:contai ...
- 编写自定义django-admin命令
Django为项目中每一个应用下的management/commands目录中名字没有以下划线开始的Python模块都注册了一个manage.py命令,我们可以利用这点来自定制一个命令(比如运行该命令 ...
- JAVA微信公众号网页开发 —— 接收微信服务器发送的消息
WeixinMessage.java package com.test; import java.io.Serializable; /** * This is an object that conta ...
- EF设计模式之code first
为了支持以设计为中心的开发流程,EF推出了以代码为中心的模式code first.我们称之为代码优先开发,代码优先的开发支持更加优美的开发流程,允许在不使用设计器或者定义一个XML映射文件的情况下进行 ...
- Linux环境下使用tcpdump抓包与下载
(1)报文抓取 tcpdump -i eno5 host 10.8.12.154 -w /test.cap -i:抓取的网卡 host:目的地址 -w:生成的文件存放路径 Ctrl+c 结束抓包,抓取 ...