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/917e1850806e3eda8b03c58486df382c to your computer and use it in GitHub Desktop.
Save jianminchen/917e1850806e3eda8b03c58486df382c to your computer and use it in GitHub Desktop.
Prepare binary search tree inorder successor problem statement
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;
}
}
The above binary search tree can be drawn in the following:
20
/ \
9 25
/ \
5 12
/ \
11 14
Given the above binary search tree, given node g has three cases. One is the root node;
the second case is in the right subtree of root node; the third case is in the left subtree
of root node. Let us look at the binary search inorder traversal sequence and see the position
of given node g in the diagram:
5 9 11 12 14 20 25
|
g
g
g
Let us use the hint:
For example, you are very lazy and busy, given node g's value is bigger than 20,
case 1: > 20, from root 20's -> 20's right child to complete the task
case 2: given node is in left subtree,
one case is that 20 > given node's successor. we can give the task to its left
subtree, and it is fine.
Function prototype:
FindBinarySearchTreeInOrderSuccessor(TreeNode root, TreeNode givenNode)
Written by Jianmin Chen 1/6/2018 5:58 PM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment