原题链接在这里:https://leetcode.com/problems/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).

For example,

   1
\
3
/ \
2 4
\
5

Longest consecutive sequence path is 3-4-5, so return 3.

   2
\
3
/
2
/
1

Longest consecutive sequence path is 2-3,not3-2-1, so return 2.

题解:

DFS bottom-up方法.

Time Complexity: O(n). Space: O(logn).

AC Java:

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int max = 0;
public int longestConsecutive(TreeNode root) {
dfs(root);
return max;
} private int dfs(TreeNode root){
if(root == null){
return 0;
} int l = dfs(root.left)+1;
int r = dfs(root.right)+1;
if(root.left != null && root.val != root.left.val-1){
l = 1;
} if(root.right != null && root.val != root.right.val-1){
r = 1;
} int curMax = Math.max(l, r);
max = Math.max(max, curMax);
return curMax;
}
}

也可采用top down的方法.

Time Complexity: O(n). Space: O(logn).

AC Java:

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int longestConsecutive(TreeNode root) {
if(root == null){
return 0;
} int [] res = new int[]{0};
dfs(root, root.val, 1, res);
return res[0];
} private void dfs(TreeNode root, int target, int count, int [] res){
if(root == null){
return;
} if(root.val != target+1){
count = 1;
}else{
count++;
} res[0] = Math.max(res[0], count);
dfs(root.left, root.val, count, res);
dfs(root.right, root.val, count, res);
}
}

跟上Binary Tree Longest Consecutive Sequence II.

类似Longest Consecutive Sequence.

LeetCode 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] 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 ...

  3. LeetCode 549. Binary Tree Longest Consecutive Sequence II

    原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence-ii/description/ 题目: G ...

  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] 298. Binary Tree Longest Consecutive Sequence_Medium tag: DFS recursive

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

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

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

  7. [LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive

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

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

  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. Kattis - register 【水】

    题意 就是 有一堆容器,然后可以执行加的操作,每个容量是 2, 3, 5, 7, 11, 13, 17, 19 然后 有进位 比如第一个 容器,到2了,就会重置为0,然后 下一个容器+ 1, 但是要保 ...

  2. loadrunner脚本篇——Run-time Settings之ContentCheck

    运用场景(很少用到): ContentCheck的设置可用来让VuGen检测存在错误的站点页面.如果被测的Web应用没有使用自定义的错误页面,那么这里不用添加规则,因为LR在回放时候,可以默认的捕捉到 ...

  3. 【LeetCode】【动态规划】表格移动问题

    前言 这里总结了两道表格移动的问题,分别是:Unique Paths 和 题一:Unique Paths 描述 A robot is located at the top-left corner of ...

  4. ubuntu: lightdm 登录root超级管理员方法

    ubuntu 12.04 lts 默认是不允许root登录的, 在登录窗口只能看到普通用户和访客登录. 以普通身份登陆Ubuntu后我们需要做一些修改,普通用户登录后, 修改系统配置文件需要切换到超级 ...

  5. [NOI2008]奥运物流

    题目 洛谷 BZOJ 做法 单环有向图毒瘤题 不考虑环和改变后继:\(\sum\limits{i=1}^n C_i\cdot K^{dep(i)}\) 考虑环无穷等比求极m:\(R(1)=\sum\l ...

  6. 机器学习相关知识整理系列之二:Bagging及随机森林

    1. Bagging的策略 从样本集中重采样(有放回)选出\(n\)个样本,定义子样本集为\(D\): 基于子样本集\(D\),所有属性上建立分类器,(ID3,C4.5,CART,SVM等): 重复以 ...

  7. VS中一个强大的功能,将Json或者XML黏贴为类

    有时候需要传递json,或者是json结构复杂,看的杂乱无章,我们可以将这个json复制下来,然后将它写成类的形式,VS中已经帮我们很好的实现了这个功能,我们只需要选择   编辑===>> ...

  8. PAT1022. Digital Library (30)

    两个坑. 一个是一直用的malloc不行了.因为malloc分配的是固定大小,之前做的题没遇到过是因为一般string都不长(malloc分配string为24个Byte),这次直接报段错误,呢们了半 ...

  9. Android 手机上获取手机当前上网IP地址

      [转] 原文              Android 手机上获取手机当前上网IP地址                (手机网关给手机号分配的IP) 每个手机上网通过移动网关的时候,网关都会给该手 ...

  10. HRBUST 2072 树上求最大异或路径值

    一个很经典的套路 思想是 F [u,v] = F[1,u] ^ F[1,v] 这样就转化成了n个数两两异或 求最大值 可以用字典树来做 每次用当前数去树中寻求最大xor值 然后把这个数字插进去 就相当 ...