MediumNo. 0549LeetCode Premium

Binary Tree Longest Consecutive Sequence II — LeetCode 549 Python Solution

Topics
Tree, Depth-First Search, Binary Tree
Reading time
5 min

The problem

Given the root of a binary tree, return the length of the longest consecutive path in the tree. A consecutive path is a path where the values of the consecutive nodes in the path differ by one.

Example

Input
root = [1,2,3]
Output
2
Explanation
The longest consecutive path is [1, 2] or [2, 1].

Python solution

PythonSolution
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def longestConsecutive(self, root: TreeNode) -> int:
        def dfs(root):
            if root is None:
                return [0, 0]
            nonlocal ans
            incr = decr = 1
            i1, d1 = dfs(root.left)
            i2, d2 = dfs(root.right)
            if root.left:
                if root.left.val + 1 == root.val:
                    incr = i1 + 1
                if root.left.val - 1 == root.val:
                    decr = d1 + 1
            if root.right:
                if root.right.val + 1 == root.val:
                    incr = max(incr, i2 + 1)
                if root.right.val - 1 == root.val:
                    decr = max(decr, d2 + 1)
            ans = max(ans, incr + decr - 1)
            return [incr, decr]

        ans = 0
        dfs(root)
        return ans

Complexity

Time
O(V+E)
Space · auxiliary
O(V)

Pattern: Tree Traversal

Choose the order — preorder, inorder, postorder, level — and the problem solves itself. LeetCode 549. Binary Tree Longest Consecutive Sequence II is filed here because LeetCode tags it Tree and Binary Tree, which is the vocabulary this hub collects.

The tree traversal guide has the Python template for the pattern and the 225 LeetCode problems that use it.

Related problems

Related problems
No.ProblemDifficulty
0543Diameter of Binary TreeEasy
0098Validate Binary Search TreeMedium
0099Recover Binary Search TreeMedium
0100Same TreeEasy
0101Symmetric TreeEasy
0104Maximum Depth of Binary TreeEasy

Frequently asked questions

How hard is LeetCode 549. Binary Tree Longest Consecutive Sequence II?

LeetCode 549. Binary Tree Longest Consecutive Sequence II is rated Medium on LeetCode.

What is the time complexity of LeetCode 549. Binary Tree Longest Consecutive Sequence II?

The Python solution on this page runs in O(V+E).

What is the space complexity of LeetCode 549. Binary Tree Longest Consecutive Sequence II?

The Python solution on this page uses O(V) auxiliary space.

What topics does LeetCode 549. Binary Tree Longest Consecutive Sequence II cover?

LeetCode 549. Binary Tree Longest Consecutive Sequence II is tagged Tree, Depth-First Search and Binary Tree on LeetCode.

Is LeetCode 549. Binary Tree Longest Consecutive Sequence II a premium problem?

Yes. LeetCode 549. Binary Tree Longest Consecutive Sequence II is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.