LintCode 614: Binary Tree Longest Consecutive Sequence II

本文探讨了在二叉树中寻找最长连续序列路径的问题,提供了详细的算法解析和C++实现代码。通过深度优先搜索(DFS)策略,递归地计算每个节点的连续递增和递减路径长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. Binary Tree Longest Consecutive Sequence II
    中文English
    Given a binary tree, find the length(number of nodes) of the longest consecutive sequence(Monotonic and adjacent node values differ by 1) path.
    The path could be start and end at any node in the tree

Example
Example 1:

Input:
{1,2,0,3}
Output:
4
Explanation:
1
/
2 0
/
3
0-1-2-3
Example 2:

Input:
{3,2,2}
Output:
2
Explanation:
3
/
2 2
2-3

解法1:代码如下:
注意Binary Tree的遍历不需要visited数组。

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */
struct DataType {
    int leftIncLen;
    int leftDecLen;
    int rightIncLen;
    int rightDecLen;
    
    DataType(int lIL = 0, int lDL = 0, int rIL = 0, int rDL = 0) : leftIncLen(lIL), leftDecLen(lDL), rightIncLen(rIL), rightDecLen(rDL) {};
};

class Solution {
public:
    /**
     * @param root: the root of binary tree
     * @return: the length of the longest consecutive sequence path
     */
    int longestConsecutive2(TreeNode * root) {
        maxTotalLength = 0;
        dfs(root);
        return maxTotalLength;
    }
private:
    DataType dfs(TreeNode * root) {
        if (!root) return DataType(0, 0, 0, 0);
        cout<<root->val<<endl;
        DataType result(1,1,1,1);

        if (!root->left && !root->right) {
            maxTotalLength = max(maxTotalLength, 1);
            return DataType(1, 1, 1, 1);
        }

        DataType resultLeft = dfs(root->left);
        DataType resultRight = dfs(root->right);

        if (root->left && root->val == root->left->val - 1) {
            result.leftDecLen = max(resultLeft.leftDecLen, resultLeft.rightDecLen) + 1;
        } else if (root->left && root->val == root->left->val + 1) {
            result.leftIncLen = max(resultLeft.leftIncLen, resultLeft.rightIncLen) + 1;
        }

        if (root->right && root->val == root->right->val - 1) {
            result.rightDecLen = max(resultRight.rightDecLen, resultRight.leftDecLen) + 1;
        } else if (root->right && root->val == root->right->val + 1) {
            result.rightIncLen = max(resultRight.rightIncLen, resultRight.leftIncLen) + 1;
        }
 
        if (!root->left) {
            maxTotalLength = max(maxTotalLength, max(result.rightDecLen, result.rightIncLen));
        } else if (!root->right) {
            maxTotalLength = max(maxTotalLength, max(result.leftDecLen, result.leftIncLen));
        } else {
            maxTotalLength = max(
                maxTotalLength, 
                max(result.rightDecLen + result.leftIncLen, 
                    result.rightIncLen + result.leftDecLen) - 1);
        }

        return result;
    }

    int maxTotalLength;
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值