Skip to content

Instantly share code, notes, and snippets.

@RP-3
Created September 15, 2020 08:01
Show Gist options
  • Save RP-3/7ae1eeda48adac5c5d47c79ebed33832 to your computer and use it in GitHub Desktop.
Save RP-3/7ae1eeda48adac5c5d47c79ebed33832 to your computer and use it in GitHub Desktop.
/**
* // Definition for a Node.
* function Node(val) {
* this.val = val;
* this.left = null;
* this.right = null;
* this.parent = null;
* };
*/
/**
* @param {Node} node
* @return {Node}
*/
var inorderSuccessor = function(node) {
let [parent, child] = [node, node.right];
if(child){ // find the left-most child of the right child
while(child){
parent = child;
child = child.left;
}
return parent;
};
[child, parent] = [node, node.parent];
while(parent){ // find the first right-parent
if(child === parent.left) return parent;
child = parent;
parent = parent.parent;
}
return null;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment