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 ...
随机推荐
- 使用自己的CSS框架(转)
[经典推介]CSS框架选择向导 不少CSS框架已经存在了一段时间,但大多数Web开发人员避免使用它们. 相反最有经验的开发者希望创建自己的CSS框架,提供个性化解决方案的优势,并减少对第三方的解决方案 ...
- C语言细节——献给入门者(一)
C语言细节——献给入门者(一) 主题 输入输出需要注意的细节 首先我们要知道大致有scanf(),printf(),getchar(),putchar(),gets(),puts()这几种输入方式. ...
- POM.xml 标签详解
pom作为项目对象模型.通过xml表示maven项目,使用pom.xml来实现.主要描述了项目:包括配置文件:开发者需要遵循的规则,缺陷管理系统,组织和licenses,项目的url,项目的依赖性,以 ...
- combobox获取值
easyui-combobox是组合框 ,既可以输入,也可以选择 获取的数据是json格式的 [{"id":"001","text":&q ...
- Aptana STUDIO 3 使用(续)
1 使用Aptana studio 3 浏览ruby代码 2 设置gbk编码.打开Aptanna Studio,选择Windows->Preferences->General->Co ...
- 博客打开慢?请禁用WordPress默认的谷歌字体!
最近几天,谷歌中国挂了之后,发现我的博客打开极慢,原以为是空间问题,可一查,发现同台服务器的用户打开并不慢,排除了空间问题后,这边查询元素发现博客打开时加载了一个链接地址“fonts.googleap ...
- 关于如何写UI及屏幕适配的一些技巧
因为公司开启了一个新的iOS项目, 所以近期比较忙, 没有更新博客,今天打算总结一下关于UI布局及屏幕适配的一些实战技巧,尤其使用纯代码,会对提升效率及代码易于维护等方面有明显帮助,这里提到的没有使用 ...
- c# string.format json字符串 formatException错误
正常字符串的string.format是没问题的但是在拼接json的字符串的时候因为里面包含了 {} 花括号 里面又嵌套了 {0} {1} {2}这些要替换的关键字 所以会报错. 经过百度. 字符串 ...
- phpstormn 中 xdebug 的详细配置2
配置PHPStorm 图1:首先配置PHP解释器的路径 图2:File>Settings>PHP>Servers,这里要填写服务器端的相关信息,name填localhost,host ...
- 已知GBK的某段码表,码表对应的字符
for i in range(0xA1A2,0xA1A5): ...