Binary Tree Paths
2015-09-16 02:41
363 查看
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
All root-to-leaf paths are:
For example, given the following binary tree:
1 / \ 2 3 \ 5
All root-to-leaf paths are:
["1->2->5", "1->3"]
public class Solution { public List<String> binaryTreePaths(TreeNode root) { List<String> answer = new ArrayList<String>(); if (root != null) searchBT(root, "", answer); return answer; } private void searchBT(TreeNode root, String path, List<String> answer) { if (root.left == null && root.right == null) answer.add(path + root.val); if (root.left != null) searchBT(root.left, path + root.val + "->", answer); if (root.right != null) searchBT(root.right, path + root.val + "->", answer); } }
相关文章推荐
- 数据挖掘与机器学习技术探索培训
- Xcode6中segue取消原push与modal选项后新增选项的解释
- 数据挖掘与机器学习技术探索培训
- LeetCode-Implement Queue using Stacks
- 欢迎使用CSDN-markdown编辑器
- 部署基于Centos7的Zimbra邮件系统-之一系统规划及DNS服务配置
- Hibernate系列之核心开发接口
- 开源一个基于天天团购的团购app
- Leetcode Maximal Square
- css
- Web API入门之移除XML格式(一)
- Hortonworks Sandbox 2.3的Ranger插件缺陷
- LeetCode Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock II
- java二维数组定义
- iOS UI基础-7.0 UIScrollView
- 用栈实现的整型数据的四则运算
- Linux 环境下 fork 函数和 exec 函数族的使用
- Coding With Bugs Flying
- 部署基于Centos7的Zimbra邮件系统-之二系统准备及安装