Skip to content

Instantly share code, notes, and snippets.

@xnorcode
Last active October 3, 2018 21:49
Show Gist options
  • Save xnorcode/153848e37f00384735aa7ba95ae7d97f to your computer and use it in GitHub Desktop.
Save xnorcode/153848e37f00384735aa7ba95ae7d97f to your computer and use it in GitHub Desktop.
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