LintCode Binary Tree Paths
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的更多相关文章
- [LintCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 / \2 ...
- 【LeetCode】257. Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
- <LeetCode OJ> 257. Binary Tree Paths
257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...
- 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 ...
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- leetcode : Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- Leetcode 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- Binary Tree Paths
Description: Given a binary tree, return all root-to-leaf paths. For example, given the following bi ...
- (easy)LeetCode 257.Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
随机推荐
- php上传绕过
URL:http://www.ichunqiu.com/section/45 php语言除了可以解析以php为后缀的文件,还可以解析php2.php3.php4.php5这些后缀的文件.
- Aggregation 聚集
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- Http 状态码对照表
1xx 消息 1. 100 Continue 2. 101 Switching Protocol 3. 102 Processing 2xx 成功 1. 200 OK ...
- WinForm关闭窗体彻底的退出方式
//System.Environment.Exit(0); //Process.GetCurrentProcess().Kill(); //System.Threading.Thread.Curren ...
- sublime 3
主题: Theme: Flatland 着色:todo Blue Dawn.tmTheme { "theme": "Flatland Dark.sublime-t ...
- nginx反向代理实现跨域请求
nginx反向代理实现跨域请求 跨域请求可以通过JSONP实现,缺点是需要修改被请求的服务器端代码进行配合,稍显麻烦通过在自己服务器上配置nginx的反向代理,可以轻松实现跨域请求 思路 示例服务器A ...
- Android SharePreference 在主进程和次进程间共享数据不同步出错
SharedPreference作为android五大存储(网络,数据库,文件,SharedPreference,contentProvider)之中最方便使用的一个,从类名上来看就不是一个存储大 ...
- SPSS常用基础操作(3)——对数据资料进行整理
在实际工作中,往往需要对取得的数据资料进行整理,使其满足特定的分析需求,下面介绍SPSS在资料整理方面的一些功能. 1.加权个案加权个案是指给不同的个案赋予不同的权重,以改变该个案在分析中的重要性.为 ...
- Unity NGUI 2D场景添加按钮
比如说先添加一个sprite 在sprite加上NGUI的 UI Button 然后重点来了 加上Box Collider 2D(重点:2D 千万不要加 Box Collider) 将Box Col ...
- C语言->实验室->指针数组
一 分析 讨论指针数组要从三个层面来考虑: 1)指针数组本身是什么 2)指针数组作为参数时的表现 3)指针数组作为返回值时的表现 二 指针数组是什么 1)指针数组--指针的集合 数组是若干元素的集合, ...