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

Especially, this path can be either increasing or decreasing. For example, [1,2,3,4] and [4,3,2,1] are both considered valid, but the path [1,2,4,3] is not valid. On the other hand, the path can be in the child-Parent-child order, where not necessarily be parent-child order.

Example 1:

Input:
1
/ \
2 3
Output: 2
Explanation: The longest consecutive path is [1, 2] or [2, 1].

Example 2:

Input:
2
/ \
1 3
Output: 3
Explanation: The longest consecutive path is [1, 2, 3] or [3, 2, 1].

Note: All the values of tree nodes are in the range of [-1e7, 1e7].

这道题是之前那道Binary Tree Longest Consecutive Sequence的拓展,那道题只让从父结点到子结点这种顺序来找最长连续序列,而这道题没有这个顺序限制,我们可以任意的拐弯,这样能找到最长的递增或者递减的路径。这道题利用回溯的思想比较容易,因为当一个结点没有子结点点时,它只需要跟其父结点进行比较,这种情况最容易处理,而且一旦叶结点处理完了,我们可以一层一层的回溯,直到回到根结点,然后再遍历的过程中不断的更新结果res即可。由于题目中说了要么是递增,要么是递减,我们不能一会递增一会递减,所以我们递增递减的情况都要统计,只是最后取最长的路径。所以我们要知道每一个结点的最长递增和递减路径的长度,当然是从叶结点算起,这样才方便往根结点回溯。当某个结点比其父结点值大1的话,说明这条路径是递增的,那么当我们知道其左右子结点各自的递增路径长度,那么当前结点的递增路径长度就是左右子结点递增路径长度中的较大值加上1,同理如果是递减路径,那么当前结点的递减路径长度就是左右子结点递减路径长度中的较大值加上1,通过这种方式我们可以更新每个结点的递增递减路径长度。在回溯的过程中,一旦我们知道了某个结点的左右子结点的最长递增递减路径长度,那么我们可以算出当前结点的最长连续序列的长度,要么是左子结点的递增路径跟右子结点的递减路径之和加1,要么事左子结点的递减路径跟右子结点的递增路径之和加1,二者中取较大值即可,参见代码如下:

解法一:

class Solution {
public:
int longestConsecutive(TreeNode* root) {
int res = ;
helper(root, root, res);
return res;
}
pair<int, int> helper(TreeNode* node, TreeNode* parent, int& res) {
if (!node) return {, };
auto left = helper(node->left, node, res);
auto right = helper(node->right, node, res);
res = max(res, left.first + right.second + );
res = max(res, left.second + right.first + );
int inc = , dec = ;
if (node->val == parent->val + ) {
inc = max(left.first, right.first) + ;
} else if (node->val + == parent->val) {
dec = max(left.second, right.second) + ;
}
return {inc, dec};
}
};

上面的方法把所有内容都写到了一个递归函数中,看起来有些臃肿。而下面这种方法分了两个递归来写,相对来说简洁一些。因为每个结点的最长连续序列长度等于其最长递增路径长度跟最长递减路径之和加1,然后分别对其左右子结点调用递归函数,取三者最大值,相当于对二叉树进行了先序遍历,参见代码如下:

解法二:

class Solution {
public:
int longestConsecutive(TreeNode* root) {
if (!root) return ;
int res = helper(root, ) + helper(root, -) + ;
return max(res, max(longestConsecutive(root->left), longestConsecutive(root->right)));
}
int helper(TreeNode* node, int diff) {
if (!node) return ;
int left = , right = ;
if (node->left && node->val - node->left->val == diff) {
left = + helper(node->left, diff);
}
if (node->right && node->val - node->right->val == diff) {
right = + helper(node->right, diff);
}
return max(left, right);
}
};

类似题目:

Binary Tree Longest Consecutive Sequence

参考资料:

https://discuss.leetcode.com/topic/85808/c-solution

https://discuss.leetcode.com/topic/85778/dfs-c-python-solutions

https://discuss.leetcode.com/topic/85764/neat-java-solution-single-pass-o-n

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之二的更多相关文章

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

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

  3. LeetCode Binary Tree Longest Consecutive Sequence

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

  4. LeetCode 549. Binary Tree Longest Consecutive Sequence II

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

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

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

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

  7. LeetCode 298. 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. [Swift]LeetCode298. 二叉树最长连续序列 $ Binary Tree Longest Consecutive Sequence

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

随机推荐

  1. java并发编程基础 --- 4.1线程简介

    一.线程简介 什么是线程: 现在操作系统在运行一个程序时,会为其创建一个进程.例如,启动一个java程序,操作系统就会创建一个java进程.现代操作系统调度的最小单元是线程,也叫轻量级进程,在一个进程 ...

  2. Sort of Python

    表达式和运算符 什么是表达式? 1+2*3 就是一个表达式,这里的加号和乘号叫做运算符,1.2.3叫做操作数.1+2*3 经过计算后得到的结果是7,就1+2*3 = 7.我们可以将计算结果保存在一个变 ...

  3. Android 源代码结构

    简介 在使用Andriod SDK进行应用程序开发的时候,我们需要对源代码进行调试,有可能需要进入到某个Android API函数内部进行跟踪调试.但是,如果目标版本的SDK没有关联对应版本的源代码的 ...

  4. 生产者/消费者问题的多种Java实现方式

    实质上,很多后台服务程序并发控制的基本原理都可以归纳为生产者/消费者模式,而这是恰恰是在本科操作系统课堂上老师反复讲解,而我们却视而不见不以为然的.在博文<一种面向作业流(工作流)的轻量级可复用 ...

  5. 第二次作业:软件分析之Steam的前世今生

    摘要:本次作业我将介绍一下Steam的相关内容,以及对Steam的相关调研测评,以及需求分析,最后就是对Steam的建议以及在中国的发展提出相应的建议 一.相关信息      Steam是一个整合游戏 ...

  6. 400多个开源项目以及43个优秀的Swift开源项目-Swift编程语言资料大合集

    Swift 基于C和Objective-C,是供iOS和OS X应用编程的全新语言,更加高效.现代.安全,可以提升应用性能,同时降低开发难度. Swift仍然处于beta测试的阶段,会在iOS 8发布 ...

  7. 【iOS】Swift ?和 !(详解)

    Swift语言使用var定义变量,但和别的语言不同,Swift里不会自动给变量赋初始值, 也就是说变量不会有默认值,所以要求使用变量之前必须要对其初始化 .如果在使用变量之前不进行初始化就会报错: [ ...

  8. C# reportview 按时间改变行颜色

    //) AND ((Day(Now()) - Day() AND (Day(Now()) - Day()),) AND (Day(Now()) - Day()) OR (Month(Now()) - ...

  9. Linq 大合集

    static void Main(string[] args) { string[] words = { "zero", "one", "two&qu ...

  10. angular2 学习笔记 の 移动端开发 ( 手势 )

    更新 : 2018-01-31 (hammer 的坑) hammer 的 pinch 在某种情况下会自动触发 panEnd,很奇葩. 解决方法就是记入时间呗 refer : https://githu ...