后继结点

后继结点_第1张图片

https://www.nowcoder.com/questionTerminal/9023a0c988684a53960365b889ceaf5e

/*
struct TreeLinkNode {
    int val;
    struct TreeLinkNode *left;
    struct TreeLinkNode *right;
    struct TreeLinkNode *next;
    TreeLinkNode(int x) :val(x), left(NULL), right(NULL), next(NULL) {
        
    }
};
*/
class Solution {
public:
    TreeLinkNode* GetNext(TreeLinkNode* pNode)
    {
        //如果当前结点的右儿子存在,则当前结点的左儿子 就是下一节点
        if(pNode->right)
        {
            pNode = pNode->right;
            if(pNode->left) pNode = pNode->left;
            return p;
        }
        //如果当前结点的右儿子不存在,就向上面找第一个满足 当前节点为 父节点的左儿子
        if(!pNode->right)
        {
            while(pNode->next && pNode == pNode->next->right) 
                pNode = pNode->next;
        }
        return p->next;
        
    }
};

后继结点_第2张图片

你可能感兴趣的:(后继结点)