Skip to content

Instantly share code, notes, and snippets.

@harit-sunrun
Created June 27, 2012 21:50
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 harit-sunrun/3007089 to your computer and use it in GitHub Desktop.
Save harit-sunrun/3007089 to your computer and use it in GitHub Desktop.
Find inOrder successor and inOrder predecessor in binary search tree (BST)
Node predecessor(Node node) {
if ((node.left == null) && (node.right==null)) {
return node;
}
if (node.right != null) {
return predecessor(node.right);
}
if (node.left != null) {
return predecessor(node.left);
}
}
Node successor(Node node) {
if ((node.left == null) && (node.right==null)) {
return node;
}
if (node.left != null) {
return successor(node.left);
}
if (node.right != null) {
return successor(node.right);
}
}
@gabhi
Copy link

gabhi commented Apr 29, 2014

is this correct? if yes can you please explain how does this work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment