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 ...
随机推荐
- MySQL建表、插入语句等
不定时更新MySQL的一些基础语句以及出现过的问题 5.10 建表语句 CREATE TABLE `policy_landvalue` ( `id` ) NOT NULL AUTO_INCREMENT ...
- MOT北京站 | 卓越研发之路:亿万级云端架构演进
随着IT行业技术周期的快速迭代,如何在激烈的市场竞争中突出重围成为了不少技术人的困惑.除了要保持良好的技术视野外,多向IT行业精英学习他们分享的实战经验,也可让技术提升,达到事半功倍的效果. MOT北 ...
- Parallel.ForEach 多线程 声明失败 "未将对象引用设置到对象的实例"
x using System; using System.Collections.Generic; namespace Parallel.ForEach { class Program { //代码结 ...
- Django之中间件&信号&缓存&form上传
中间件 1.中间件是什么? 中间件顾名思义,是介于request与response处理之间的一道处理过程,相对比较轻量级,并且在全局上改变django的输入与输出.因为改变的是全局,所以需要谨慎实用, ...
- 15.4-uC/OS-III资源管理(二值信号量)
互斥信号量是 uC/OS 操作系统的一个内核对象, 与多值信号量非常相似,但它是二值的,只能是 0 或 1,所以也叫二值信号量, 主要用于保护资源. 1.如果想要使用互斥信号量,就必须事先使能互斥信号 ...
- 【Common】NO.81.Note.1.Common.1.002-【文章摘要】
1.0.0 Summary Tittle:[Common]NO.81.Note.1.Common.1.002-[文章摘要] Style:Common Series:Common Since:2018- ...
- ajax 显示,删除,批量删除,修改反填功能实现
1.页面代码 <body> <h1>显示所有员工信息</h1> <input id="Button1" type="button ...
- python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告
1.环境准备: python3.6 requests xlrd openpyxl HTMLTestRunner_api 2.目前实现的功能: 封装requests请求方法 在excel填写接口请求参数 ...
- 关于12C中optimizer_adaptive_features参数介绍
optimizer_adaptive_features参数在OLAP数据仓库环境中可以获得较好的效果,实际在重上传轻查询的OLTP系统上,可以关闭这项新功能. 其主要功能是为了在语句执行过程中实时收集 ...
- beego 初体验 - 基础模块 - config, httplibs, logs
beego 的基础模块支持了一些web开发常用的功能. 配置,http操作库,日志 配置模块: 这是我的配置文件 如何读取: httplibs:这是一个利用 httplibs 发起 get 请求的示例 ...