</>

298. Binary Tree Longest Consecutive Sequence

Medium
Trees Time O(N) Space O(H)
↗ Open on LeetCode Source: Misc

Quick review

Pattern
Trees
Time
O(N)
Space
O(H)

Standard Solution (C++)

SummaryGiven a binary tree, find the length of the longest path where each node's value is exactly one greater than its parent's value (consecutive increasing sequence along a root-to-leaf direction).

Approach
  • Perform a DFS traversal, passing down the current streak length and the parent's value at each step.
  • At each node, extend the streak by 1 if the node's value equals parent + 1; otherwise reset the streak to 1.
  • Track the global maximum streak seen so far via a reference parameter, updating it after each node is processed.
  • This works because consecutive sequences can only grow downward (parent to child), so a single top-down pass visits every possible streak exactly once in O(n) time.
#include <algorithm>
using namespace std;

// Definition for a binary tree node.
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
    // DFS helper: parentVal is the value of the calling node,
    // streak is the current consecutive length, maxLen tracks the global best.
    void dfs(TreeNode* node, int parentVal, int streak, int& maxLen) {
        if (!node) return;

        // Extend streak if consecutive, otherwise restart at 1.
        streak = (node->val == parentVal + 1) ? streak + 1 : 1;
        maxLen = max(maxLen, streak);

        dfs(node->left,  node->val, streak, maxLen);
        dfs(node->right, node->val, streak, maxLen);
    }

public:
    int longestConsecutive(TreeNode* root) {
        int maxLen = 0;
        // Seed parentVal as root->val - 1 so the root always starts a streak of 1.
        if (root) dfs(root, root->val - 1, 0, maxLen);
        return maxLen;
    }
};
Time O(n) Space O(h), where h is the height of the tree (O(n) worst case for a skewed tree, O(log n) for a balanced tree)

A canonical reference solution. Problem statement © LeetCode — see the official link above.

My Solution (C++)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    void dfs(TreeNode* root, int pVal, int curLen, int& curMax){
        if(!root){
            return;
        }
        int rVal=root->val;
        if(rVal==pVal+1){
            curLen+=1;
        }
        else{
            curLen=1;
        }
        curMax=std::max(curMax, curLen);
        dfs(root->left, rVal, curLen, curMax);
        dfs(root->right, rVal, curLen, curMax);
    }
    int longestConsecutive(TreeNode* root) {
        int curMax=0;
        dfs(root, root->val-1, 0, curMax);
        return curMax;
    }
};

// Time Complexity: O(N)
// Space Complexity: O(H) where H is the height of the tree
Time O(N) Space O(H)

My own solution and notes, as I wrote them.

▶ Run it — editable in-browser playground with a sample driver C++

Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.

C++ ▶ Runnable Auto-generated driver with sample input — unverified.

Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.

Related — same pattern