Leetcode 285 Solution
This article provides solution to leetcode question 285 (inorder-successor-in-bst)
Access this page by simply typing in "lcs 285" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/inorder-successor-in-bst
Solution
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p)
{
TreeNode* res = NULL;
while (root)
{
if (root->val > p->val)
{
res = root;
root = root->left;
}
else
root = root->right;
}
return res;
}
};