[抄题]:

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: 3 Explanation: Longest consecutive sequence path is 3-4-5, so return 3.

Example 2:

Input:

   2
\
3
/
2
/
1 Output: 2 Explanation: Longest consecutive sequence path is 2-3, not 3-2-1, so return 2.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩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最长连续序列的更多相关文章

  1. [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 ...

  2. LeetCode 298. Binary Tree Longest Consecutive Sequence

    原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...

  3. 298. Binary Tree Longest Consecutive Sequence

    题目: Given a binary tree, find the length of the longest consecutive sequence path. The path refers t ...

  4. [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 ...

  5. [leetcode]128. Longest Consecutive Sequence最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...

  6. [Leetcode] Longest consecutive sequence 最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  7. 128. Longest Consecutive Sequence最长连续序列

    [抄题]: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. 基于ajax 验证表单是否被占用----------------附:10.25日总结

    总得来说,今天的主要工作是注册页面的处理, 1.判断 用户名与密码是否为空值 ,两次密码框输入的值是否相同.判断邮箱过程中,有使用到正则表达式 2.用户名是否使用过,有用到了json与ajax的知识. ...

  2. vue watch 可以监听子组件props里面属性的改变

    子组件watch 可以监听其props里面属性的改变 当changeFather导致calm改变时,会执行console.log('props change');

  3. easyui-datebox 点击事件

    <div class="form-group col-xs-5 col-md-5 col-lg-6" style = "margin-left: 0px;" ...

  4. Spring Cloud(Dalston.SR5)--Config 集群配置中心

    Spring Cloud Config 是一个全新的项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,他分为服务端和客户端两个部分.服务端也称为分布式配置中心,是一个独立的微服务 ...

  5. 防爆等级介绍 - IP65防爆等级和dIIBT4防爆等级的有什么区别?

    IP65 IP是Ingress Protection的缩写,IP等级是针对电气设备外壳对异物侵入的防护等级,如:防爆电器,防水防尘电器,来源是国际电工委员会的标准IEC 60529,这个标准在2004 ...

  6. 【spring】之xml和Annotation,Bean注入的方式

    基于xml形式Bean注入 @Data @AllArgsConstructor @NoArgsConstructor public class PersonBean { private Integer ...

  7. Cookie深度解析

    最近在公司做了Web端单点登录(SSO)功能,基于Cookie实现,做完之后感觉有必要总结一下,本文着重讲解Cookie,下文会说明单点登录的实现方案. Cookie简介 众所周知,Web协议(也就是 ...

  8. 如何在ORACLE中查询某一用户下所有的空表

    先分析表 select 'analyze table '||table_name||' compute statistics;' from user_tables; 把查询结果依次执行 把所有表分析一 ...

  9. 工具函数(获取url , 时间格式化,随机数)

    (function(window,$){ function Tools() { } // url Tools.prototype.readUrlToParams = function() { var ...

  10. 【Noip模拟 20160929】树林

    题目描述 现在有一片树林,小B很想知道,最少需要多少步能围绕树林走一圈,最后回到起点.他能上下左右走,也能走对角线格子. 土地被分成RR行CC列1≤R≤50,1≤C≤501≤R≤50,1≤C≤50,下 ...