298. Binary Tree Longest Consecutive Sequence最长连续序列
[抄题]:
Given a binary tree, find the length of the longest consecutive sequence path.
The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from parent to child (cannot be the reverse).
Example 1:
Input: 1
\
3
/ \
2 4
\
5 Output:3Explanation: Longest consecutive sequence path is3-4-5, so return3.
Example 2:
Input: 2
\
3
/
2
/
1 Output: 2 Explanation: Longest consecutive sequence path is2-3, not3-2-1, so return2.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
不知道怎么比较左右两边的最大值:居然连dc的精髓都忘了,就写一个表达式,然后我用Max,让它自己搜出来就好了
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
val是上一个节点的val,所以root要低一级。主函数中是root.left root.right
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
需要统计个数时,参数就是节点数count
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public int longestConsecutive(TreeNode root) {
//corner case
if (root == null) return 0;
//return
return Math.max(dfs(root.left, root.val, 1), dfs(root.right, root.val, 1));
}
public int dfs(TreeNode root, int val, int count) {
//exit when root is null
if (root == null) return count;
//renew count, left, right
count = (root.val == val + 1) ? count + 1 : 1;
int left = dfs(root.left, root.val, count);
int right = dfs(root.right, root.val, count);
//compare and return
return Math.max(Math.max(left, right), count);
}
}
298. Binary Tree Longest Consecutive Sequence最长连续序列的更多相关文章
- [LeetCode] 298. Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- LeetCode 298. Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- 298. Binary Tree Longest Consecutive Sequence
题目: Given a binary tree, find the length of the longest consecutive sequence path. The path refers t ...
- [LC] 298. Binary Tree Longest Consecutive Sequence
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [leetcode]128. Longest Consecutive Sequence最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- [Leetcode] Longest consecutive sequence 最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 128. Longest Consecutive Sequence最长连续序列
[抄题]: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...
- [LeetCode] 549. Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之 II
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- [LintCode] 619 Binary Tree Longest Consecutive Sequence III 二叉树最长连续序列 III
Given a k-ary tree, find the length of the longest consecutive sequence path. The path could be star ...
随机推荐
- VS2010添加虚拟机发布的WebService引用
首先,WebServer已在虚拟机中完成发布.在网页中浏览可以看到如下所示内容 需要注意的是在发布时要给网站设置IP地址.如果在添加网站时没有设置,之后可以在网站绑定中进行修改.步骤如下: 1.选中网 ...
- MySQL Error--Error Code
mysql error code(备忘) 1005:创建表失败 1006:创建数据库失败 1007:数据库已存在,创建数据库失败 1008:数据库不存在,删除数据库失败 1009:不能删除数据库文件导 ...
- Django学习笔记之数据库-QuerySet_API
QuerySet API 我们通常做查询操作的时候,都是通过模型名字.objects的方式进行操作.其实模型名字.objects是一个django.db.models.manager.Manager对 ...
- C语言数组指针
C语言中的数组指针与指针数组: ·数组指针一.区分 首先我们需要了解什么是数组指针以及什么是指针数组,如下: int *p[5];int (*p)[5];数组指针的意思即为通过指针引用数组,p先和*结 ...
- python运行过程
程序执行过程 PyCodeObject:PyCodeObject则是Python编译器真正编译成的结果. 当python程序运行时,编译的结果则是保存在位于内存中的PyCodeObject中,当Pyt ...
- STM32作为主设备,Arduino作为从设备进行IIC通讯的注意要点
近日公司的项目重心要往米思齐的Arduino图形化编程上转移了,需要我将STM32和Arduino的IIC通讯调通.之前Arduino并没怎么使用过,仅仅是将超声波的代码移植成TOF激光测距而已.网上 ...
- js window.location用法
<script> //设置或获取 href 属性中跟在问号后面的部分. console.log(window.location.search)//设置或获取对象指定的文件名或路径conso ...
- 如何让Excel单元格中的名字分散对齐
1 操作方式 开始->对齐方式->对齐->水平对齐->分散对齐(缩进) 2 优势 不会破坏数据的有效性
- 部署MySQL自动化运维工具inception+archer
***************************************************************************部署MySQL自动化运维工具inception+a ...
- layui流加载+h5自带模板
@{ ViewBag.Title = "服务列表"; Layout = "~/Areas/hahaha/Views/Shared/_Head.cshtml"; ...