class Solution {
public:
TreeNode* inorderSuccessor(TreeNode* p)
{
// 当此结点有右子树的时候,那么右子树的最左边就是答案
if(p->right != nullptr) {
auto res = p->right;
while(res->left != nullptr) res = res->left;
return res;
}
// 当p没有右子树的时候,
// 那么 其祖先节点中,p在左侧的father即是答案
while(p->father != nullptr && p->father->right == p) p = p->father;
return p->father;
}
};
求关注