[LeetCode]257. Binary Tree Paths
2017-02-03 19:10
260 查看
https://leetcode.com/problems/binary-tree-paths/
找出二叉树根节点到叶子节点的所有路径
public class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> res = new LinkedList();
if (root != null) {
search(root, res, "");
}
return res;
}
private void search(TreeNode root, List<String> res, String part) {
if (root.left == null && root.right == null) {
res.add(part + root.val);
}
if (root.left != null) {
search(root.left, res, part + root.val + "->");
}
if (root.right != null) {
search(root.right, res, part + root.val + "->");
}
}
}
找出二叉树根节点到叶子节点的所有路径
public class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> res = new LinkedList();
if (root != null) {
search(root, res, "");
}
return res;
}
private void search(TreeNode root, List<String> res, String part) {
if (root.left == null && root.right == null) {
res.add(part + root.val);
}
if (root.left != null) {
search(root.left, res, part + root.val + "->");
}
if (root.right != null) {
search(root.right, res, part + root.val + "->");
}
}
}
相关文章推荐
- EL 表达式
- 给IT同行的健康养生建议
- 1023. Have Fun with Numbers (20)
- JS的单引号,双引号问题
- 1051. Pop Sequence (25)PAT甲级
- C++ cout输出,如何set精度
- Tkinter控件加载图片
- 完成管理平台的bootstrap界面布局
- processing 游戏编程实例
- 3.3 1!到n!的和
- iptables.sh 初始化防火墙配置
- Spring4笔记6--Spring与AOP
- Android中APK直接通过JNI访问驱动
- 双叉积——拉格朗日公式证明
- 高通camera vendor层logic
- git常用操作之查看改动日志和进行版本切换(四)
- 彻底理解javascript的回调函数
- LeetCode 61. Rotate List
- 如何用visual studio 2010 新建一个C程序项目
- 如何通过giihub下载软件