8. 二叉树的下一个结点

/*
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;
            while(pNode->left != nullptr){
                pNode = pNode->left;
            }
            return pNode;
        }else{
            while(pNode->next){
                if(pNode == pNode->next->left){
                    return pNode->next;
                }
                pNode = pNode->next;
            }
            return nullptr;
        }
        
    }
};
posted @ 2021-03-28 12:03  rxh1999  阅读(37)  评论(0编辑  收藏  举报