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 ...
随机推荐
- Sring 类的例子
public class ZongHe { public static void main(String[] args) { function1(); funct ...
- layer loading层 的设置
/* shadeClose 类型:Boolean 默认:true,是否点击遮罩时关闭层 */ var tishi = layer.open({ shadeClose: false ,type: 2 , ...
- RFID世界网
RFID世界网 地址:http://www.rfidworld.com.cn/NFC/
- consul服务配置维护
1.命令参数 -advertise:通知展现地址用来改变我们给集群中的其他节点展现的地址,默认情况下-bind地址就是展现地址,然而也存在一些路由地址是不能受约束的,这时候会激活一个不同的地址来供应, ...
- MongoDB复制集原理、环境配置及基本测试详解
一.MongoDB复制集概述 MongoDB复制集实现了冗余备份和故障转移两大功能,这样能保证数据库的高可用性.在生产环境,复制集至少包括三个节点,其中一个必须为主节点,一个从节点,一个仲裁节点.其中 ...
- git diff 结果分析
git diff 的5个使用场景: 1.staging area和working area的文件 (无其他参数时) git diff 2.master分支和working area的文件 (用ma ...
- android 知识汇总
1.assets:不会在R.java文件下生成相应的标记,assets文件夹可以自己创建文件夹,必须使用AssetsManager类进行访问,存放到这里的资源在运行打包的时候都会打入程序安装包中, 2 ...
- 【JVM】-NO.114.JVM.1 -【JDK11 HashMap详解-3-put-treeifyBin()-AVL】
Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total Hours:1 Degree Of Diffculty:5 Degree Of ...
- JMeter已传值但是提示为空
登录时已经传值了,可是一直提示为空 解决:在在请求的url中拼接上参数
- 20165321 测试-3-ch02