w3resource

Java Exercises: Find the length of the longest consecutive sequence path of a given binary tree

Java Basic: Exercise-184 with Solution

Write a Java program to find the length of the longest consecutive sequence path of a given binary tree.

Note: The longest consecutive path need to be from parent to child.

Sample Binary tree:

Java Basic Exercises: Find the length of the longest consecutive sequence path of a given binary tree.

Result:

Java Basic Exercises: Find the length of the longest consecutive sequence path of a given binary tree.

Sample Solution:

Java Code:

import java.util.*;
public class Solution {  
   public static void main(String[] args) {
        TreeNode a = new TreeNode(1);
        a.right = new TreeNode(3);
        a.right.left = new TreeNode(2);
		a.right.right = new TreeNode(4);
		a.right.right.right = new TreeNode(5);
		a.right.right.right.right = new TreeNode(6);
		System.out.println("Length of the longest consecutive sequence path: "+longest_Consecutive(a));
    }
  public static int longest_Consecutive(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int result = diffn(root, 1) + diffn(root, -1);
        return Math.max(result, Math.max(longest_Consecutive(root.left), longest_Consecutive(root.right)));
    }
    private static int diffn(TreeNode tnode, int diff) {
        if (tnode == null) {
            return 0;
        }
        int left_depth = 0, right_depth = 0;
        // check node
        if (tnode.left != null && tnode.val - tnode.left.val == diff) {
            left_depth = diffn(tnode.left, diff) + 1;
        }
        if (tnode.right != null && tnode.val - tnode.right.val == diff) {
            right_depth = diffn(tnode.right, diff) + 1;
        }
        return Math.max(left_depth, right_depth);
    }
}

Sample Output:

Length of the longest consecutive sequence path:  4

Flowchart:

Flowchart: Java exercises: Find the length of the longest consecutive sequence path of a given binary tree.

Java Code Editor:

Company:  Google NetEase

Contribute your code and comments through Disqus.

Previous: Write a Java program to accept a positive number and repeatedly add all its digits until the result has only one digit.
Next: Write a Java program to check if two given strings are isomorphic or not.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

Java: Tips of the Day

Java: Anagrams

Generates all anagrams of a string.

public static List<String> anagrams(String input) {
    if (input.length() <= 2) {
        return input.length() == 2
                ? Arrays.asList(input, input.substring(1) + input.substring(0, 1))
                : Collections.singletonList(input);
    }
    return IntStream.range(0, input.length())
            .mapToObj(i -> new SimpleEntry<>(i, input.substring(i, i + 1)))
            .flatMap(entry ->
                    anagrams(input.substring(0, entry.getKey()) + input.substring(entry.getKey() + 1))
                            .stream()
                            .map(s -> entry.getValue() + s))
            .collect(Collectors.toList());
}

Ref: https://bit.ly/3rvAdAK