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 – BFS

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 – DFS

class Solution {
    int max;
 
    public int longestConsecutive(TreeNode root) {
        helper(root);
        return max;
    }
 
    private int helper(TreeNode t){
        if(t==null){
            return 0;
        }
 
        int leftMax = helper(t.left);
        int rightMax = helper(t.right);
 
        int leftTotal = 0;
        if(t.left == null){
            leftTotal = 1;
        }else if(t.val+1 == t.left.val){
            leftTotal = leftMax+1;    
        }else{
            leftTotal = 1;
        }
 
        int rightTotal = 0;
        if(t.right == null){
            rightTotal = 1;
        }else if(t.val+1 == t.right.val){
            rightTotal = rightMax+1;    
        }else{
            rightTotal = 1;
        }
 
        max = Math.max(max, leftTotal);
        max = Math.max(max, rightTotal);
 
        int longer = Math.max(leftTotal, rightTotal);   
 
        return longer;
    }    
}

7 thoughts on “LeetCode – Binary Tree Longest Consecutive Sequence (Java)”


  1. public int longestConsecutive(Node root) {
    if (root == null)
    return 1;

    return preOrder(root, null, -1);
    }

    private int preOrder(Node n, Node p, int pCurr) {
    int cMax = 1;
    if(p != null && n.value == p.value + 1)
    cMax = pCurr + 1;
    int max = cMax;

    if (n.left != null) {
    int childMax = preOrder(n.left, n, cMax);
    max = Math.max(max, childMax);
    }

    if (n.right != null) {
    int childMax = preOrder(n.left, n, cMax);
    max = Math.max(max, childMax);
    }

    return max;
    }

  2. Simple solution:


    static int max = 1;
    private static int getMax(TreeNode root) {

    helper(root, 1);
    return max;
    }

    private static void helper(TreeNode root, int count) {

    if(root == null) return;

    max = Math.max(count, max);

    if(root.left != null)
    helper(root.left, (root.val + 1 == root.left.val) ? count + 1 : 1);

    if(root.right != null)
    helper(root.right, (root.val + 1 == root.right.val) ? count + 1 : 1);
    }

  3. No use of calculating the max and needs to be ignored

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

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

  5. 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.

Leave a Comment