Inorder Successor in BST II
Description
null
Solution(javascript)
/**
* // Definition for a Node.
* function Node(val) {
* this.val = val;
* this.left = null;
* this.right = null;
* this.parent = null;
* };
*/
/**
* @param {Node} node
* @return {Node}
*/
const inorderSuccessor = function (node) {
if (node.right) {
let current = node.right
while (current.left) {
current = current.left
}
return current
}
let current = node
while (current && current.parent) {
if (current.parent.left === current) {
return current.parent
}
current = current.parent
}
return null
}