剑指Offer5
二叉树的下一个结点
https://www.nowcoder.com/practice/9023a0c988684a53960365b889ceaf5e?tpId=13&tqId=11210&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github
题目
思路
Code
class Solution {
public:
TreeLinkNode* GetNext(TreeLinkNode* pNode)
{
if(!pNode) //输入测试
return nullptr;
if(pNode->right != nullptr)
{
pNode = pNode->right;
while(pNode->left != nullptr)
{
pNode = pNode->left;
}
return pNode;
}
else{
while(pNode->next != nullptr)
{
TreeLinkNode* parent = pNode->next;
if(parent->left == pNode)
return parent;
pNode = pNode->next;
}
}
return nullptr;
}
};
查看18道真题和解析