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.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int res = 0;
public int longestConsecutive(TreeNode root) {
if (root == null) {
return res;
}
helper(root, root.val, 0);
return res;
} private void helper(TreeNode root, int num, int cur) {
if (root == null) {
return;
}
if (root.val == num + 1) {
cur += 1;
} else {
cur = 1;
}
res = Math.max(res, cur);
helper(root.left, root.val, cur);
helper(root.right, root.val, cur);
}
}

[LC] 298. Binary Tree Longest Consecutive Sequence的更多相关文章

  1. LeetCode 298. Binary Tree Longest Consecutive Sequence

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

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

  3. 298. Binary Tree Longest Consecutive Sequence最长连续序列

    [抄题]: Given a binary tree, find the length of the longest consecutive sequence path. The path refers ...

  4. 298. Binary Tree Longest Consecutive Sequence

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

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

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

  7. LeetCode Binary Tree Longest Consecutive Sequence

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

  8. [Locked] Binary Tree Longest Consecutive Sequence

    Binary Tree Longest Consecutive Sequence Given a binary tree, find the length of the longest consecu ...

  9. [LeetCode] Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之二

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

随机推荐

  1. ref与out区别

    ref与out   out.ref都是传递引用(内存地址),使用后都将改变原来参数的数值.   ref 当调用方法时,在方法中会对ref传入的参数数值进行改变,若使用ref参数,则方法定义和调用方法都 ...

  2. 《打造扛得住的MySQL数据库架构》第4章 MySQL数据库结构优化

    4-1 数据库结构优化介绍 良好的数据库逻辑设计和物理设计是数据库获得高性能的基础. 1.减少不必要的数据冗余. 2.尽量避免数据维护中出现更新,插入和删除异常. 插入异常:如果表中的某个实体随着另一 ...

  3. 小程序转义字符去空格 

    <text ].specInfo}}</text> 在微信小程序开发过程中,有时候会用到常用的一些特殊字符如:‘<’.‘>’.‘&’.‘空格’等,微信小程序同样支 ...

  4. UML-GRASP后4种模式

    1.多态 1).什么是多态 问题:if-else耦合度过高 解决: 方法1:接口 方法2:超类里需多态的方法前加上{abstract} 2).相关模式 防止异变 大量GoF,如适配器(Adapter) ...

  5. GCC常见命令汇总

    int main() { test(); } man.c如上: #include <stdio.h> void test() { printf("test\n"); } ...

  6. urllib简单介绍

    # urllib简介: 1.urllib模块是Python的一个请求模块 2.Python2中是urllib和urllib2相结合实现请求的发送. Python3中统一为urllib库 3.urlli ...

  7. 201604-2 俄罗斯方块 Java

    大家谁能帮我看看是哪里不对,提交到系统中是0分,在Eclipse中可以得出例子中的结果 思路: 题目中有两个关键点:如何模拟下落的过程,如何判断方块下落在哪里停止. 在数据的存储上,需要保存整个&qu ...

  8. Java web之jsp,xml(2020.1.7)

    1.xml文档规则 xml声明 字符集 xml元素的基本规则: 合法标签名 嵌套子元素 空元素

  9. Fractal Dimension|Relative Complexity|CG含量|重复序列|

    生物信息学-序列拼接方法 物理学方法 Fractal Dimension of Exon and Intron Sequences --------------CGCGGCGTGTGTTATA --- ...

  10. linux服务器开放防火墙和端口,以及查询状态

    自己搞一个自己网站时候,购买的阿里云服务器,发现部署项目访问不到,首先需要确认入站规则是否配置. 一.安全组列表添加 1.打开安全组列表 2.添加入站规则 3.添加安全组规则 二.通过防火墙,开启端口 ...