C++ 代码
blablabla
/
* 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 == NULL)
return NULL;
if(p->right !=NULL){
//have right tree
TreeNode* pr = p->right;
while(pr->left!=NULL){
pr = pr->left;
}
return pr;
}
else if (p->father!=NULL &&p->father->left == p ){
return p->father;
//don't have right tree
}
else if (p->father!=NULL&&p->father->right == p){
TreeNode* fa = p -> father;
while(fa->father != NULL &&fa->father->right == fa){
fa = fa->father;
}
return fa->father;
}
else
return NULL;
}
};