Question

Given a binary tree, return all root-to-leaf paths.

For example, given the following binary tree:

   1
/ \
2 3
\
5

All root-to-leaf paths are:

["1->2->5", "1->3"]

Solution -- Recursive

We can not apply inorder traversal here because it only show path from root to leaf node as required. Therefore, we use recursive way.

We consider two situation:

1. Current node is leaf. Add string to result.

2. Current node has children. Go on.

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> result = new ArrayList<String>();
if (root == null)
return result;
dfs(root, result, new StringBuilder());
return result;
} private void dfs(TreeNode root, List<String> result, StringBuilder current) {
if (root == null)
return;
if (root.left == null && root.right == null) {
current.append(root.val);
result.add(current.toString());
return;
} current.append(root.val);
current.append("->"); StringBuilder tmp1 = new StringBuilder(current);
StringBuilder tmp2 = new StringBuilder(current);
if (root.left != null)
dfs(root.left, result, tmp1);
if (root.right != null)
dfs(root.right, result, tmp2);
}
}

Binary Tree Paths 解答的更多相关文章

  1. [LintCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 /   \2 ...

  2. LintCode Binary Tree Paths

    Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Given the following binary tre ...

  3. 【LeetCode】257. Binary Tree Paths

    Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...

  4. &lt;LeetCode OJ&gt; 257. Binary Tree Paths

    257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...

  5. 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 ...

  6. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  7. leetcode : Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  8. Leetcode 257. Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  9. Binary Tree Paths

    Description: Given a binary tree, return all root-to-leaf paths. For example, given the following bi ...

随机推荐

  1. /proc/uptime

    在Linux中,我们常常会使用到uptime命令去看看系统的运行时间,它与一个文件有关,就是/proc/uptime.这个文件里的两个参数所代表的意义如下. [root@app ~]#cat /pro ...

  2. web应用,我们需要了解什么?

        对于前端开发来说,web应用我们并不陌生.今天想要讨论一下,在开发一个web应用的时候,我们需要一些基本的知识储备.我们知道,一个web应用脱离不了(request)请求和响应(respons ...

  3. Python 入门教程 9 ---- A Day at the Supermarket

    第一节 1 介绍了for循环的用法 for variable in values: statement 2 for循环打印出列表的每一项 for item in [1 , 2 , 3]: print ...

  4. 动态SQL的执行,注:exec sp_executesql 其实可以实现参数查询和输出参数的

    本文转自:http://www.cnblogs.com/hnsdwhl/archive/2011/07/23/2114730.html 当需要根据外部输入的参数来决定要执行的SQL语句时,常常需要动态 ...

  5. Go语言程序的状态监控

    Go是很实在的编程语言,从一开始就提供了很详细的运行状态信息.产品上线后的调优和排查疑难杂症都得靠这些状态信息.这边总结一些我们项目里用到的状态监控手段. pprof Go自带了一个pprof工具,这 ...

  6. 【COCOS2DX-对28游戏开发】 Cocos2d-x-3c 道路设计 CocosBase CocosNet CocosWidget

    原文链接:http://blog.csdn.net/cocosviva/article/details/18970717 另一个比較不错的cocos2dx扩展库:https://github.com/ ...

  7. HTTP协议报文格式

    HTTP协议报文格式 接下来我们看看HTTP协议(Hypertext Transfer Protocol――超文本传输协议)浏览器端(客户端)向WEB服务器端访问页面的过程和HTTP协议报文的格式. ...

  8. for-in用法

    var nyc = {     fullName: "New York City",     mayor: "Bill de Blasio",     popu ...

  9. django Model模型二及Model模型对数据库的操作

    在django模型中负责与数据库交互的为Model层,Model层提供了一个基于orm的交互框架 一:创建一个最基本的Model from __future__ import unicode_lite ...

  10. 【转】Visual Studio 2010在数据库生成随机测数据

    测试在项目中是很重要的一个环节,在Visual Studio 2010中,在测试方面已经有很好的支持了,比如有单元测试,负载测试等等.在数据测试的方面,Visual Studio 2010,还支持对数 ...