257. Binary Tree Paths返回所有深度优先的遍历
[抄题]:
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
1
/ \
2 3
\
5
All root-to-leaf paths are:
["1->2->5", "1->3"]
[暴力解法]:
时间分析:
空间分析:
[奇葩corner case]:
[奇葩输出]:
看到["1->2->5", "1->3"]就吓懵了,其实就是字符串数组啊
[思维问题]:
以为这下需要汇总再返回了,其实不用,返回也是参数化了,在函数参数中进行的
[一句话思路]:
- 左子树非空就递归左边,右子树非空就递归右边。划分的条件是左右,以前用过但是不懂。
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- 叶子结点象征一条路的结束,可以把路添加到ans中。
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
- 叶子结点象征一条路的结束,可以把路添加到ans中。
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
二叉树的原理是先走到最底层叶子结点,然后返回根节点开始调用
[关键模板化代码]:
void findBT(TreeNode root, String path, List<String> ans) {
if (root.left == null && root.right == null)
ans.add(path + root.val);//add here since it's an sign of end
if (root.left != null) findBT(root.left, path + root.val + "->", ans);
if (root.right != null) findBT(root.right, path + root.val + "->", ans);
}
path ans都作为helper的参数
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
/**
* 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) {
//corner case
List<String> ans = new ArrayList<>();
if (root == null) {
return ans;
}
findBT(root, "", ans);
return ans;
} void findBT(TreeNode root, String path, List<String> ans) {
if (root.left == null && root.right == null)
ans.add(path + root.val);//add here since it's an sign of end
if (root.left != null) findBT(root.left, path + root.val + "->", ans);
if (root.right != null) findBT(root.right, path + root.val + "->", ans);
}
}
257. Binary Tree Paths返回所有深度优先的遍历的更多相关文章
- <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
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
- 257. Binary Tree Paths
题目: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree ...
- LeetCode 257. 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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [LeetCode] 257. 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 ...
- Leetcode 257. 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 二叉树 DFS
找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...
随机推荐
- Java API 操作 Mongodb
本次测试环境使用一台ip为 192.168.2.23 的虚拟机 一.依赖 <dependency> <groupId>org.mongodb</groupId> & ...
- undefined vs. null
undefined vs. null 一.相似性 在JavaScript中,将一个变量赋值为undefined或null,老实说,几乎没区别. var a = undefined; var a = n ...
- oracle的时间
今天发现之前从oracle导出的数据时间格式很奇怪: "ACTIVETIME","ACTIVITYID" "2018-08-10 06:08:43&q ...
- 搭建基于hyperledger fabric的联盟社区(二) --环境配置
接下来讲一下在本地测试区块链网络的过程.我要部署的是2peer+1orderer架构,所以需要准备三台虚拟机,为了方便起见可以先把一台配置好,然后复制出剩余两台即可.搭建虚拟机我用的是virtualb ...
- python稀疏矩阵得到每列最大k项的值,对list内为类对象的排序(scipy.sparse.csr.csr_matrix)
print(train_set.tdm) print(type(train_set.tdm)) 输出得到: (0, 3200) 0.264940780338 (0, 1682) 0.356545827 ...
- [python] 使用scikit-learn工具计算文本TF-IDF值
在文本聚类.文本分类或者比较两个文档相似程度过程中,可能会涉及到TF-IDF值的计算.这里主要讲述基于Python的机器学习模块和开源工具:scikit-learn. 希望文章对你有所帮 ...
- thinkphp 3.2.3 计划任务具体实现实例教程
thinkphp 3.2.3 计划任务具体实现实例教程 很多情况下,我们网站都会用到计划任务即定时更新做一些处理,类似Discuz后台的计划任务,比如更新每日发帖数目等等! 这里TP也是可以实现的,首 ...
- css选择器30种
CSS 选择器是一种模式,用于选择需要添加样式的元素.平时使用最多也是最简单的就是 #id..class 和标签选择器,在 CSS 中还有很多更加强大更加灵活的选择方式,尤其是在 CSS3 中,增加了 ...
- linux 下java环境的配置
注意:这里选择下载jdk并自行安装,而不是通过源直接安装(apt-get install) 1.下载jkd( http://www.oracle.com/technetwork/java/javase ...
- RAC环境TNS-12541报错处理
按照前文所述搭建好RAC环境后,发现在rac2上面无法查看到listener的状态,如下: [oracle@rac2 ~]$ lsnrctl status LSNRCTL for Linux: Ver ...