LeetCode – Binary Tree Longest Consecutive Sequence (Java)

Given a binary tree, find the length of the longest consecutive sequence path.

The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from parent to child (cannot be the reverse).

Java Solution 1 - Queue

public int longestConsecutive(TreeNode root) {
    if(root==null)
        return 0;
 
    LinkedList<TreeNode> nodeQueue = new LinkedList<TreeNode>();
    LinkedList<Integer> sizeQueue = new LinkedList<Integer>();
 
    nodeQueue.offer(root);
    sizeQueue.offer(1);
    int max=1;
 
    while(!nodeQueue.isEmpty()){
        TreeNode head = nodeQueue.poll();
        int size = sizeQueue.poll();
 
        if(head.left!=null){
            int leftSize=size;
            if(head.val==head.left.val-1){
                leftSize++;
                max = Math.max(max, leftSize);
            }else{
                leftSize=1;
            }
 
            nodeQueue.offer(head.left);
            sizeQueue.offer(leftSize);
        }
 
        if(head.right!=null){
            int rightSize=size;
            if(head.val==head.right.val-1){
                rightSize++;
                max = Math.max(max, rightSize);
            }else{
                rightSize=1;
            }
 
            nodeQueue.offer(head.right);
            sizeQueue.offer(rightSize);
        }
 
 
    }
 
    return max;
}

Java Solution 2 - Recursion

public class Solution {
    int max=0;
 
    public int longestConsecutive(TreeNode root) {
        helper(root);
        return max;
    }
 
    public int helper(TreeNode root) {
        if(root==null)
            return 0;
 
        int l = helper(root.left);
        int r = helper(root.right);
 
        int fromLeft = 0;
        int fromRight= 0;
 
        if(root.left==null){
            fromLeft=1;
        }else if(root.left.val-1==root.val){
            fromLeft = l+1;
        }else{
            fromLeft=1;
        }
 
        if(root.right==null){
            fromRight=1;
        }else if(root.right.val-1==root.val){
            fromRight = r+1;
        }else{
            fromRight=1;
        }
 
        max = Math.max(max, fromLeft);
        max = Math.max(max, fromRight);
 
        return Math.max(fromLeft, fromRight);
    }
 
}
Category >> Algorithms  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>
  • Subodh Karwa

    No use of calculating the max and needs to be ignored

    max = Math.max(max, fromLeft);
    max = Math.max(max, fromRight);

  • Aditya Vutukuri

    I guess there is a mistake. The tree here is considered as BST instead of BT. The sequence could be increasing or decreasing.

  • Avinash Sharma

    I believe there is a mistake in first solution in the part of the code where:

    if(head.val = head.left.val-1) it should be if(head.val == head.left.val+1) as the left value should be one less only then consecutive sequence makes sense.