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. ajax是异步的,异步取数据,如何能保证数据是存在的。

    https://segmentfault.com/q/1010000002964172 ajax是异步的,所以aa()在执行的时候,return的那个result本身就是空的,在此后的某些秒以后,re ...

  2. iOS - Xcode 配置

    1.Xcode 配置 1.1 OS X 1)main 文件注释修改路径: /Applications(应用程序) ▸ Xcode.app ▸ Contents ▸ Developer ▸ Librar ...

  3. iOS MD5加密字符串

    参考:http://stackoverflow.com/questions/1524604/md5-algorithm-in-objective-c 在线测试MD5:http://www.cmd5.c ...

  4. linux 下安装安装rz/sz命令

    一.软件安装 root 账号登陆后,依次执行以下命令: cd /tmp wget http://www.ohse.de/uwe/releases/lrzsz-0.12.20.tar.gz http:/ ...

  5. 20160928_CentOS6.4x64_Oracle11gR2x64

    1.“Oracle Database 11g Release 2” 下载地址: http://www.oracle.com/technetwork/database/enterprise-editio ...

  6. Java 实现word 中写入文字图片的解决方案

    JAVA生成WORD文件的方法目前有以下两种方式: 一种是jacob 但是局限于windows平台 往往许多JAVA程序运行于其他操作系统 在此不讨论该方案; 一种是poi但是他的excel处理很程序 ...

  7. alu features menu

    可以把所有的custom menu和menu link移植到新的环境下,并且增加新的menu link不会吧module变成overridden, 只需到时recreate生成一个新的module包, ...

  8. gameui-for-phaser-js更新到最新版本

    Hola Cantk是一个功能强大的游戏引擎,它拥有丰富的GUI控件,配合Hola Studio可以快速开发出游戏的UI界面.Phaser是一个流行而且强大的游戏引擎,但是它的GUI控件很少,而且缺乏 ...

  9. android 5.0 水波纹 实现

    1. 定义一个普通圆角背景的xml; rounded_corners.xml <?xml version="1.0" encoding="utf-8"?& ...

  10. flask 程序结构概括

    以此结构为例,这个小项目是<Flask Web开发:基于python的web应用开发实战>第一部分结束后的代码框架 第一层 有app.tests.migrations三个文件夹和confi ...