/
* 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 fun(TreeNode q){
if(q->left==NULL)return q;
return fun(q->left);
}
TreeNode inorderSuccessor(TreeNode p) {
if(p->right!=NULL)return fun(p->right);
while(p->father!=NULL&&p->father->right==p){
p=p->father;
}
if(p==NULL||p->father==NULL)return NULL;
if(p->father->left==p){
return p->father;
}
return NULL;
}
};