Find given node in a BST
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
... | |
// 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