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 ...
随机推荐
- [LeetCode系列]子集枚举问题[无重复元素]
给定一组数(未排序), 求它们的所有组合可能. 如给定{1 2 3}, 返回: [ [] [1] [2] [3] [1 2] [1 3] [2 3] [1 2 3] ] 算法思路: 对数组排序, 从小 ...
- appium+python自动化41-切换webview时候报chromedriver版本问题
前言 用appium切换webview的时候报chrome和chromedriver版本的问题:session not created exception: Chrome version must b ...
- Java-Runoob-高级教程:Java 泛型
ylbtech-Java-Runoob-高级教程:Java 泛型 1.返回顶部 1. Java 泛型 Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检 ...
- configure: error: jpeglib.h not found.
编译出现错误: configure: error: jpeglib.h not found. 解决方法:yum install libjpeg libjpeg-devel -y libjpeg-dev ...
- 3dmax卡通渲染插件pencil+渲染线框
转自:http://www.cr173.com/soft/179512.html http://www.psoft.co.jp/jp/ 官网和YTB有 2代的视频教程,平均每个2分钟长,无解说,是日文 ...
- PHP for和foreach的区别
首先,我们先准备两个用于遍历的数组: $arr1=array(1=>'a', 3=>22, 5=>'b', 4=>'c', 8=>'d'); $arr2=array('a ...
- verilog 之数字电路 边沿检测电路
由代码可知:此边沿检测电路是由两个触发器级联而成,sign_c_r 输出是sign_c_r2的输入.并且有异步复位端没有使能端.最后输出:由触发器的输出取反和直接输出相与.如下的RTL图.
- SQL Server 2008系统信息查询常用命令 查看表大小、记录数等
1.返回所有数据库信息(数据库名,创建日期,存储路径等). use master; GO select * from dbo.sysdatabases 2.返回当前数据库所有对象(可根据type字 ...
- Android:不同drawable文件夹的区别
4.0后,新建android工程,会自动生成drawable,drawalbe-ldpi,drawable-mdpi,drawable-hdpi,drawable-xhdpi,drawable-xxh ...
- Julia - 字符串
字符 字符使用单引号括起来,字符是 32 位整数 julia> 'a' 'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase) ju ...