509. Inorder Successor in BST II
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the in-order successor of a given node in a Binary Search Tree (BST). The in-order successor is the node that would be visited immediately after the given node in an in-order traversal. A key feature of this problem is that each node has a pointer to its parent, which simplifies finding the successor, especially when the given node does not have a right subtree. The solution should work without comparing node values, as hinted by the follow-up.
Solution
Inorder Successor in BST II in Python
"""
# Definition for a Node.
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
self.parent = None
"""
class Solution:
def inorderSuccessor(self, node: 'Node') -> 'Optional[Node]':
# Case 1: Node has a right subtree.
# The successor is the leftmost node in the right subtree.
if node.right:
successor = node.right
while successor.left:
successor = successor.left
return successor
# Case 2: Node has no right subtree.
# Go up using parent pointers until we find a node which is a left child of its parent.
# The parent of that node is the successor.
p = node
while p.parent and p == p.parent.right:
p = p.parent
return p.parent
Inorder Successor in BST II in Java
/*
// Definition for a Node.
class Node {
public int val;
public Node left;
public Node right;
public Node parent;
};
*/
class Solution {
public Node inorderSuccessor(Node node) {
if (node == null) {
return null;
}
// Case 1: Node has a right subtree.
// The successor is the leftmost node in the right subtree.
if (node.right != null) {
Node successor = node.right;
while (successor.left != null) {
successor = successor.left;
}
return successor;
}
// Case 2: Node has no right subtree.
// Go up using parent pointers until we find a node which is a left child of its parent.
// The parent of that node is the successor.
Node p = node;
while (p.parent != null && p == p.parent.right) {
p = p.parent;
}
return p.parent;
}
}
Inorder Successor in BST II in C++
/*
// Definition for a Node.
class Node {
public:
int val;
Node* left;
Node* right;
Node* parent;
};
*/
class Solution {
public:
Node* inorderSuccessor(Node* node) {
if (!node) {
return nullptr;
}
// Case 1: Node has a right subtree.
// The successor is the leftmost node in the right subtree.
if (node->right) {
Node* successor = node->right;
while (successor->left) {
successor = successor->left;
}
return successor;
}
// Case 2: Node has no right subtree.
// Go up using parent pointers until we find a node which is a left child of its parent.
// The parent of that node is the successor.
Node* p = node;
while (p->parent != nullptr && p == p->parent->right) {
p = p->parent;
}
return p->parent;
}
};
Inorder Successor in BST II in TypeScript
/**
* Definition for _Node.
* class _Node {
* val: number
* left: _Node | null
* right: _Node | null
* parent: _Node | null
*
* constructor(v: number) {
* this.val = v;
* this.left = null;
* this.right = null;
* this.parent = null;
* }
* }
*/
function inorderSuccessor(node: _Node | null): _Node | null {
if (!node) {
return null;
}
// Case 1: Node has a right subtree.
// The successor is the leftmost node in the right subtree.
if (node.right) {
let successor: _Node = node.right;
while (successor.left) {
successor = successor.left;
}
return successor;
}
// Case 2: Node has no right subtree.
// Go up using parent pointers until we find a node which is a left child of its parent.
// The parent of that node is the successor.
let p: _Node = node;
while (p.parent && p === p.parent.right) {
p = p.parent;
}
return p.parent;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.