Skip to content

Instantly share code, notes, and snippets.

@kanrourou
Created December 1, 2017 04:54
/**
* 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) {
if(!root)return nullptr;
if(root->val > p->val)
{
auto findLeft = inorderSuccessor(root->left, p);
return findLeft? findLeft: root;
}
else if(root->val <= p->val)
return inorderSuccessor(root->right, p);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment