Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Find given node in a BST
...
// Find a node in the BST
static Node find(int val){
return find(root, val);
}
// Find a node in the BST recursive helper method
private static Node find(Node current, int val){
// return Null if BST does not contain given node
if(current == null) return null;
// return current node if matches given node
if(current.data == val) return current;
// traverse the remain BST via node children
return current.data > val ? find(current.left, val) : find(current.right, val);
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment