The Idea
The key idea is use a recursive function to start searching from most-left child to most-right child. Then, it returns 1 when current value or root data equals target.
static int isPresent(Node root, int val){
// For your reference
// class Node {
// Node left, right;
// int data;
// Node(int newData) {
// left = right = null;
// data = newData;
// }
// }
if (root == null)
{
return 0;
}
else
{
if (root.data == val)
{
return 1;
}
else
{
int left = isPresent(root.left, val);
int right = isPresent(root.right, val);
if (left == 1 || right == 1)
return 1;
else
return 0;
}
}
}