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

                                                                                                         ...

  2. 【HDU1102】Constructing Roads(MST基础题)

    最小生成树水题.prim一次AC #include <iostream> #include <cstring> #include <cstdlib> #includ ...

  3. 对每个用户说hello

    #!/bin/bash #对每个用户说hello #用户数 Lines=`wc -l /etc/passwd | cut -d' ' -f1` $Lines`; do echo "Hello ...

  4. iOS 面试常见问题最全梳理

    序言 目前形势,参加到iOS队伍的人是越来越多,甚至已经到供过于求了.今年,找过工作人可能会更深刻地体会到今年的就业形势不容乐观,加之,培训机构一火车地向用人单位输送iOS开发人员,打破了生态圈的动态 ...

  5. web前端之 CSS引入第三方插件

    引入第三方图标插件 - fontawesome 官网地址:http://fontawesome.io/ 1.下载图标插件包 下载地址:https://codeload.github.com/FortA ...

  6. boost::asio 的同、异步方式

    转自:http://blog.csdn.net/zhuky/archive/2010/03/10/5364574.aspx Boost.Asio是一个跨平台的网络及底层IO的C++编程库,它使用现代C ...

  7. 密码算法详解——AES

    0 AES简介 1997年1月2号,美国国家标准技术研究所宣布希望征集一个安全性能更高的加密算法(AES)[3],用以取代DES.我们知道DES的密钥长度是64 bits,但实际加解密中使用的有效长度 ...

  8. Windows - 远程桌面无证书

    可以从命令行启动远程桌面,输入:mstsc /v:地址:端口 /admin

  9. UIScrollView的基本使用和一些常用代理方法

    - (void)viewDidLoad { [super viewDidLoad]; scrollView = [[UIScrollView alloc] initWithFrame:CGRectMa ...

  10. Android方法的传递值及其改变

    MainActivity如下: package cn.testchangevar; import android.os.Bundle; import android.view.View; import ...