Leetcode 285 Solution

This article provides solution to leetcode question 285 (inorder-successor-in-bst)

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; } };