Elements Between K1 and K2
Send Feedback
Given a Binary Search Tree and two integers k1 and k2, find and print the elements which are in range k1 and k2 (both inclusive).
Print the elements in increasing order.
Input format :
Output Format :
Sample Input :
Sample Output :
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;
}
}
*/
public static void printNodeFromK1ToK2(BinaryTreeNode<Integer> root,int k1,int k2){
if(root==null)
{
return;
}
if(root.data<k1)
{
printNodeFromK1ToK2(root.right,k1,k2);
}
else if(root.data>k2)
{
printNodeFromK1ToK2(root.left,k1,k2);
}
else
{
//System.out.print(root.data+" ");
printNodeFromK1ToK2(root.left,k1,k2);
System.out.print(root.data+" ");
printNodeFromK1ToK2(root.right,k1,k2);
//System.out.print(root.data+" ");
}
}
}
No comments:
Post a Comment