/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode *father;
* TreeNode(int x) : val(x), left(NULL), right(NULL), father(NULL) {}
* };
*/
class Solution {
public:
TreeNode* inorderSuccessor(TreeNode* p) {
if(!p) return nullptr;
if(p->right){
TreeNode* curr = p->right;
while (curr->left)
curr = curr->left;
return curr;
}
TreeNode* curr = p;
TreeNode* father = p->father;
while(father && curr == father->right){
curr = father;
father = father->father;
}
return father;
}
};
要考虑多种情况