Ninja
March 09, 2020
Longest consecutive Sequence
Longest consecutive Sequence
Send Feedback
Line 1 : Integer n, Size of array
Line 2 : Array elements (separated by space)
13
2 12 9 16 10 5 3 20 25 11 1 8 6
8
9
10
11
12
7
3 7 2 1 9 8 1
7
8
9
Explanation: Sequence should be of consecutive numbers. Here we have 2 sequences with same length i.e. [1, 2, 3] and [7, 8, 9], but output should be [7, 8, 9] because the starting point of [7, 8, 9] comes first in input array.
7
15 24 23 12 19 11 16
15
16
N:L:x,R:y
Input format :
Elements in level order form (separated by space). If any node does not have left or right child, take -1 in its place.
Output format : Each level linked list is printed in new line (elements separated by space).
5 6 10 2 3 -1 -1 -1 -1 -1 9 -1 -1
5
6 10
2 3
9
import java.util.ArrayList;import java.util.*;public class Solution {/* Binary Tree Node class** class BinaryTreeNode<T> {T data;BinaryTreeNode<T> left;BinaryTreeNode<T> right;public BinaryTreeNode(T data) {this.data = data;}}*//* class Node<T> {T data;Node<T> next;Node(T data){this.data = data;}}*/
Line 1 : Integer n (Size of array)
Line 2 : Array elements (separated by space)
BST elements (in pre order traversal, separated by space)
7
1 2 3 4 5 6 7
4 2 1 3 6 5 7
Line 1 : Elements in level order form (separated by space)
(If any node does not have left or right child, take -1 in its place)
Line 2 : Two Integers k1 and k2
Required elements (separated by space)
8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1
6 10
6 7 8 10
Line 1 : Elements in level order form (separated by space)
(If any node does not have left or right child, take -1 in its place)
Line 2 : Integer k
Node with data k
8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1
2
2
8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1
12
(empty)
public class Solution {
/* Binary Tree Node class
*
* class BinaryTreeNode<T> {
T data;
BinaryTreeNode<T> left;
BinaryTreeNode<T> right;
public BinaryTreeNode(T data) {
this.data = data;
}
}
*/
//Main code
public static BinaryTreeNode<Integer> searchInBST(BinaryTreeNode<Integer> root , int k){
if(root==null)
{
return null;
}
if(root.data==k)
{
return root;
}
if(k<root.data)
{
return searchInBST(root.left ,k);
}
return searchInBST(root.right ,k);
}
}