Skip to content

Instantly share code, notes, and snippets.

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 jianminchen/b5352711e69472b5b0b9710a55c0ab42 to your computer and use it in GitHub Desktop.
Save jianminchen/b5352711e69472b5b0b9710a55c0ab42 to your computer and use it in GitHub Desktop.
Binary search tree inorder successor - peer discussion - January 6 2018 - 1:31 pm
/*
https://i.stack.imgur.com/OWd4M.jpg
Given the node with value 14, its inorder successor is 20; given the node with value 25, its inorder successor is null. given the node with value 9, its inorder successor is 11.
internal class TreeNode
{
public int Value { get; set; }
public TreeNode Left { get; set; }
public TreeNode Right { get; set; }
public TreeNode(int number)
{
Value = number;
}
}
20
/ \
9 25
/ \
5 12
/ \
11 14
5 9 11 12 14 20 25
|
g
g
g
> 20, root 20's -> 20's right child to complete the task
case of given node is in left subtree, 20 > given node's successor
give the task to its left subtree, and it is fine.
left subtree,
FindBinarySearchTreeInOrderSuccessor(TreeNode root, TreeNode givenNode)
public TreeNode helper(TreeNode root, TreeNode p){
TreeNode ret=null;
while(root!=null){
if(root.val>p.val){
ret=root;
root=root.left;
}else{
root=root.right;
}
}
return ret;
}
find next greater elemnt in BST
https://leetcode.com/problems/count-of-smaller-numbers-after-self/
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment