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多线程:死锁

    周末看到一个用jstack查看死锁的例子.昨天晚上总结了一下jstack(查看线程).jmap(查看内存)和jstat(性能分析)命令.供大家参考  1.Jstack 1.1 jstack能得到运行j ...

  2. Matlab绘图基础——其他三维图形(绘制填充的五角星)

    其他三维图形 %绘制魔方阵的三维条形图 subplot(2,2,1); bar3(magic(4));   %以三维杆图形式绘制曲线y=2sin(x) subplot(2,2,2); y=2*sin( ...

  3. Jmeter 多用户同时登陆

    在做性能测试的时候,很多情况需要多用户同时登录,下单,那怎么实现多用户的同时登录呢 可以通过CSV Data Set Config组件实现参数化登录 1.新建一个存放用户名和密码的文件, 和jmete ...

  4. java.text.DateFormat 多线程并发问题

    在日常开发中,java.text.DateFormat 应该算是使用频率比较高的一个工具类,经常会使用它 将 Date 对象转换成字符串日期,或者将字符串日期转化成 Date 对象.先来看一段眼熟的代 ...

  5. 新事物学习---WebApp移动端手势Hammer

    花落水流红,闲愁万种,无语怨东风. Hammer介绍 Hammer库是一个移动端手势库,移动端的手势操作(比如touch,tap,拖动,滑动等等)都可以用这个库,而我们不用关心,它的底层方案具体是怎么 ...

  6. 用Python满足满足自己的“小虚荣”

    首先声明,学习这个只是为了好玩,只是为了好玩,并不是想用这个弄虚作假,做一些不好的事情!一心想做技术人,自制自治! 我们有时候发布一篇日志,或者是一篇博文,总希望自己的浏览量能高点,这样看起来也倍有面 ...

  7. 团队第1次作业:Our Team TAH

     Team named TAH    不管一个人多么有才能,但是集体常常比他更聪明和更有力. --奥斯特洛夫斯基     *introduce team and teamate 先说说TAH的含义,是 ...

  8. Web前端性能分析

    Web前端性能通常上代表着一个完全意义上的用户响应时间,包含从开始解析HTML文件到最后渲染完成开始的整个过程,但不包括在输入url之后与服务器的交互阶段.下面是整个过程的各个步骤: 开始解析html ...

  9. 完美解决某法院HP EVA8400删除VDISK问题

    [故障描述] 某地法院一台HP EVA8400存储,2组扩展柜,物理磁盘由12个1T FATA磁盘(AG691A 454414-001)和10个300G 15K FC磁盘(AG690A 454411- ...

  10. Nginx在windows环境下的安装与简单配置

    版权声明:本文为博主原创文章,未经博主允许不得转载. 一. 下载并安装Nginx 去Nginx官网下载 我这里选取nginx/Windows-1.10.3版本,下载后解压出来即可,解压出来的路径不能含 ...