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 ...
随机推荐
- [转]使用python爬取东方财富网机构调研数据
最近有一个需求,需要爬取东方财富网的机构调研数据.数据所在的网页地址为: 机构调研 网页如下所示: 可见数据共有8464页,此处不能直接使用scrapy爬虫进行爬取,因为点击下一页时,浏览器只是发起了 ...
- 经典算法二分查找循环实现Java版
二分查找 定义 二分查找(Binary Search)又称折半查找,它是一种效率较高的查找方法. 要求 (1)必须采用顺序存储结构 (2)必须按关键字大小有序排列 查找思路 首先将给定值K,与表中中间 ...
- (整理)在REHL6.5上部署ASP.NET MVC
最近项目要使用Linux服务器(REHL6.5)+MySQL,因此特尝试操作. 1 Linux 安装Jexus 1.1 下载Jexus 因为服务器没有安装Xwindows,Jexus的下载又出现问题, ...
- PHP chdir函数:改变当前的目录
PHP chdir函数的作用是改变当前的目录,这里主机吧详细介绍下chdir函数的用法,并列举使用chdir函数的例子. chdir定义和用法: chdir() 函数改变当前的目录. chdir实例: ...
- LINUX PID 1 和 SYSTEMD
要说清 Systemd,得先从Linux操作系统的启动说起.Linux 操作系统的启动首先从 BIOS 开始,然后由 Boot Loader 载入内核,并初始化内核.内核初始化的最后一步就是启动 in ...
- [持续交付实践] pipeline使用:Multibranch Pipeline
前言 在探讨multiBranch Pipeline之前,很有必要先探讨下如何制定有效的代码分支管理规范,使用高效的版本控制系统,并对构建产物及其依赖进行管理.我们首先要强调,需要进行版本控制的不仅是 ...
- 转载:c++深拷贝和浅拷贝
文章来自:http://blog.csdn.net/u010700335/article/details/39830425 C++中类的拷贝有两种:深拷贝,浅拷贝:当出现类的等号赋值时,即会调用拷贝函 ...
- RedHat 7.0更新升级openSSH7.4p1
由于目前服务器上ssh版本较低,存在安全漏洞,需要升级到最新版本. 系统版本:RedHat 7.0 旧openSSH版本:6.4p1 新openSSH版本:7.4p1 升级方式:源码安装 安装操作步骤 ...
- Python模块定义和使用
Python中所谓的模块就是一个Python文件,一个abc.py的文件就是一个名字叫abc的模块,一个xyz.py的文件就是一个名字叫xyz的模块.模块由代码.函数或类组成.编程中使用模块不仅可以提 ...
- 自动滚动标签marquee
<marquee>标签,它是成对出现的标签,首标签<marquee>和尾标签</marquee>之间的内容就是滚动内容.<marquee>标签的属性主要 ...