Binary Tree Paths

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

Given the following binary tree:

    1
/ \
2 3
\
5

All root-to-leaf paths are:

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

As mentioned in the problem we want to get all root-to-leaf paths. Because all the paths need to be found and I was considering recursively finding this could be very efficently due to reducing the possibility that missing some case.

To recursively do it, three things need to be considered:

1. What is the relationship of problem of this this tree and its two subtrees?

2. Is the problem size reducing?

3. What is the base case?

So

1. the traversing done for this tree is the traversing of this node plus left tree and right tree. We need to put the node's value to a string and when the traverse is reaching the leaves and/or null values we should return.

2. yes

3. Base case is the tree is only one node or null;

 /**
* 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
*/
public List<String> binaryTreePaths(TreeNode root) {
// Write your code here
List<String> result = new ArrayList<String>();
if (root == null) {
return result;
}
traverse(root,String.valueOf(root.val),result);
return result;
} public void traverse(TreeNode root, String path, List<String> lists) {
if (root == null) {
return;
}
if (root.left == null && root.right == null) {
lists.add(path);
return;
}
if (root.left != null) {
traverse(root.left,path+"->"+String.valueOf(root.left.val),lists);
}
if (root.right != null) {
traverse(root.right,path+"->"+String.valueOf(root.right.val),lists);
}
}
}

The trick for this problem is that the string is used to store the current path values and the adding new node values should be done in the parameters in the function calls of the left and right subtrees. This is not what I was expected because it is confused to think things together but it could be something new to learn.

LintCode 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. 【LeetCode】257. Binary Tree Paths

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

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

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

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

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

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

  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 257. Binary Tree Paths

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

  8. Binary Tree Paths

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

  9. (easy)LeetCode 257.Binary Tree Paths

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

随机推荐

  1. 【转】对硬盘进行分区时,GPT和MBR区别。

    在Windows 8或8.1中设置新磁盘时,系统会询问你是想要使用MBR还是GPT分区.GPT是一种新的标准,并在逐渐取代MBR. GPT带来了很多新特性,但MBR仍然拥有最好的兼容性.GPT并不是W ...

  2. FreeMarker标签介绍

    转自:http://www.blogjava.net/kxbin/articles/366505.html FreeMarker标签使用 一.FreeMarker模板文件主要有4个部分组成  1.文本 ...

  3. Json2JsonArray JsonArray2StringArray

    public String[] json2JsonArray(String str){ JSONArray jsonArray = JSONArray.fromObject(str); String[ ...

  4. SQL*Loader实验笔记【二】

      所有SQL*Loader实验笔记 实验案例总结(1-7):     SQL*Loader实验笔记[一] 实验案例总结(8-13):   SQL*Loader实验笔记[二] 实验案例总结(14-19 ...

  5. spring-servlet.xml简单示例

    spring-servlet.xml简单示例 某个项目中的spring-servlet.xml 记下来以后研究用 <!-- springMVC简单配置 --> <?xml versi ...

  6. 用substr()截取中文出现乱码的解决方法

    截取中文字符串时出现乱码(使用substr()函数) 程序一:PHP截取中文字符串方法 function msubstr($str, $start, $len) {    $tmpstr = &quo ...

  7. 使用Jsoup 抓取页面的数据

    需要使用的是jsoup-1.7.3.jar包   如果需要看文档我下载请借一步到官网:http://jsoup.org/ 这里贴一下我用到的 Java工程的测试代码 package com.javen ...

  8. sprintf函数

    sprintf函数用法举例 #include<stdio.h> int main() { //1.连接字符串 char a1[] = {'A', 'B', 'C', 'D', 'E', ' ...

  9. Java 获取网络重定向文件的真实URL

    其实Java 使用HttpURLConnection下载的的时候,会自动下载重定向后的文件,但是我们无法获知目标文件的真实文件名,文件类型,用下面的方法可以得到真实的URL,下面是一个YOUKU视频的 ...

  10. SSL/TLS 高强度加密: 常见问题解答

    关于这个模块 mod_ssl 简史 mod_ssl会受到Wassenaar Arrangement(瓦森纳协议)的影响吗? mod_ssl 简史 mod_ssl v1 最早在1998年4月由Ralf ...