[C++] 纯文本查看 复制代码 TreeNode* inorderSuccessor(TreeNode *p) {
if (p==nullptr)
return nullptr;
if (p->right != nullptr) {
p = p->right;
while (p->left != nullptr)
p = p->left;
return p;
}
while (p->father != nullptr&&p->father->right == p) {
p = p->father;
}
if (p->father != nullptr)
return p->father;
else return nullptr;
}
int main() {
TreeNode *Node1 = new TreeNode(2);
TreeNode *Node2 = new TreeNode(1);
TreeNode *Node3 = new TreeNode(3);
//优化一下代码,建议这么写
Node2->father = Node1;
Node3->father = Node1;
Node1->left = Node2;
Node1->right = Node3;
/*不知道你想表达什么,函数参数类型是指针类型,你想传什么类型?*/
TreeNode*result = inorderSuccessor(Node1);//就是不知道为什么一直只能用地址,而导致函数无法传送参数而变成零。
if(result!=nullptr)
std::cout << result->val;
delete Node1;
delete Node2;
delete Node3;
} |