Created
September 15, 2020 08:01
-
-
Save RP-3/7ae1eeda48adac5c5d47c79ebed33832 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* // 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