Inorder Successor in BST
Description
null
Solution(javascript)
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/** 可以直接 inOrder 遍历然后查找
* @param {TreeNode} root
* @param {TreeNode} p
* @return {TreeNode}
*/
const inorderSuccessor = function (root, p) {
const target = p.val
let current = root
let result = null
while (current) {
if (current.val > target) {
result = current
current = current.left
} else {
current = current.right
}
}
return result
}