Error:
cannot solve it. Use inorder traverse need O(n).

So we use the property of BST, when meet the target p, it have two cases:

  • p has right subtree, first move root = root.right, then find its leftmost node
  • p has not right subtree, return its parent