Last active
May 27, 2018 04:31
-
-
Save thmain/a21d4dca75400fbadbd8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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