AcWing 19. 二叉树的下一个节点
原题链接
中等
作者:
chipizz
,
2019-02-14 21:02:39
,
所有人可见
,
阅读 1257
/**
* 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->right){
p = p->right;
if (p->left == NULL && p->father->left == p) {
p = p->father;
return p;
}
while (p->left) p = p->left;
return p;
}
else {
while (p->father != NULL && p->father->left != p) p = p->father;
if (p->father == NULL) return NULL;
if (p->father->left == p) return p->father;
}
}
};