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 ...
随机推荐
- 远程复制数据免登录 rsync 和 scp
一.备用机上(用于存放备份的机器) 和 目标机上(需要备份的服务器 ,如 246) 都需要安装 : yum install -y rsync 二.备用机上运行命令: -t rsa Generat ...
- C语言 数组排序法总结
//快速排序法 void QuickSort(int *a, int left, int right) { int i = left; int j = right; int k = a[left]; ...
- C#String地址、拼接性能学习
String类型不可变.定义string变量时会在堆上分配存储空间,而对该变量进行值变更时会重新分配一个存储空间,且保留原存储空间. 测试思路:获取string类型变量值变更前后的存储空间地址,判断地 ...
- Java中split的用法
Java中的我们可以利用split把字符串按照指定的分割符进行分割,然后返回字符串数组,下面是string.split的用法实例及注意事项:java.lang.string.splitsplit 方法 ...
- struts2学习(13)struts2文件上传和下载(1)
一.Struts2文件上传: 二.配置文件的大小以及允许上传的文件类型: 三.大文件上传: 如果不配置上传文件的大小,struts2默认允许上传文件最大为2M: 2097152Byte: 例子实现 ...
- 第六章 hbase shell 命令
hbase shell命令 描述 alter 修改列族(Column Family)模式 count 统计表中行的数量 create 创建表 ...
- android:修改PagerTabStrip中的背景颜色,标题字体的样式、颜色和图标以及指示条的颜色
1.修改PagerTabStrip中的背景颜色 我们在布局中直接设置background属性即可: <android.support.v4.view.ViewPager android:id=& ...
- jquery easy ui 的formatter 格式化函数代码
- leetcode498
public class Solution { public int[] FindDiagonalOrder(int[,] matrix) { ); ); + col - ; var ary = ne ...
- SpringData
1.什么是SpringData?Spring Data 项目的目的是为了简化构建基于 Spring 框架应用的数据访问计数,包括非关系数据库.Map-Reduce 框架.云数据服务等等:另外也包含对关 ...