Search In BST
Send Feedback
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);
}
}
About Ninja
Soratemplates is a blogger resources site is a provider of high quality blogger template with premium looking layout and robust design
No comments:
Post a Comment