Skip to content

Instantly share code, notes, and snippets.

@thmain
Last active May 27, 2018 04:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/a21d4dca75400fbadbd8 to your computer and use it in GitHub Desktop.
Save thmain/a21d4dca75400fbadbd8 to your computer and use it in GitHub Desktop.
//Approach will be very simple, do any of the tree traversal and check if given element is present.
public class T_SearchElementInTree {
public static boolean isPresent(Node root, int x) {
if (root != null) {
// check if current node has the element we are looking for
if (root.data == x) {
return true;
} else {
// check if the sub trees
return isPresent(root.left, x) || isPresent(root.right, x);
}
}
return false;
}
public static void main(String[] args) {
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
System.out.println("Does 4 exist in the tree: " + isPresent(root, 4));
System.out.println("Does 7 exist in the tree: " + isPresent(root, 7));
}
}
class Node {
int data;
Node left;
Node right;
public Node(int data) {
this.data = data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment