剑指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;
    }
};
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务