To search in binary tree

Following function help to search element in binary tree
Pre Requisite: Java ,Binary tree.
Level: medium
It is a simple method which will return the Search result in binary tree.
We have followed the recursive approach to solve this problem.
Binary tree is tree , where each node have at most 2 child.

As the parameter ,we will pass the Object which is the root of the binary tree.
“searchVariable” is the value which we have to search
METHOD:

public boolean searchInBinaryTree(Node N){

boolean result = false;
if(N == null)
return false;

if(N.value == searchVariable)
return true;

if(N.left != null)
result = searchInBinaryTree(N.left);

if(N.right != null)
result = searchInBinaryTree(N.right);

return result;
}
Note : link below will give you more information and help you in understanding in better way.

Advertisements