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. C++库汇总

    C++库汇总 C++类库介绍再次体现了C++保持核心语言的效率同时大力发展应用库的发展趋势!!在C++中,库的地位是非常高的.C++之父 Bjarne Stroustrup先生多次表示了设计库来扩充功 ...

  2. IntelliJ IDEA中使用综合使用Maven和Struts2

    在Intellij IDEA中手动使用Maven创建Web项目并引入Struts2 创建一个新的Maven项目 建好项目之后点击左下角的enable auto import 项目部署 在Moudule ...

  3. 个推,手机推送api的使用

    个推的作用:可以为手机端的app使用者推送消息,而不是通过手机上的app对用户发送消息.所以用户是被动的接收信息.当然不只是只有对用户弹出窗口的这种方式,也可以把信息推送给app,让app决定对用户实 ...

  4. osg渲染数据高程文件

    使用gdal解析DEM文件,将高程数据转换为HeightField对象,然后在osg渲染. 1 源代码 #include <gdal_priv.h> #include <osgVie ...

  5. struts(二) ---中参数传值

    struts中参数传值的方式有 种: 第一种:直接通过属性来传值 第二种: 第三种:

  6. 爬虫:获取多次跳转后的页面url

    <?php $url = "xxxx"; $xx = get_headers($url,true); var_dump($xx); ?> Nbimer主页 剖面加阴影应 ...

  7. Python中类的特殊方法详解

    本文和大家分享的主要是python语言中类的特殊方法相关用法,希望对大家有帮助. 构造序列 1._len_(self) 2._getitem_(self,key) 3._setitem_(self,k ...

  8. 自定义N维空间数组

    class Space : IEnumerable<Space> { public object Filler { get { return filler ?? (filler = Top ...

  9. HR函数学习01——创建组织单位

    创建组织单位: RH_OBJECT_CREATE REPORT ZLYHR01. DATA:LS_OBJ TYPE OBJEC, LV_STU TYPE GDSTR-SVECT, LV_TIT TYP ...

  10. 关于ecshop的那些故事

    1.php下foreach()错误提示Warning: Invalid argument supplied for foreach() 错误提示:Warning: Invalid argument s ...