剑指offer---二叉树的下一个结点

/*
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==NULL)
        {
            return NULL;
        }
        
        if(pNode->right!=NULL)
        {
            pNode=pNode->right;
            while(pNode->left!=NULL)
            {
                pNode=pNode->left;
            }
            return pNode;
        }
        while(pNode->next!=NULL)
        {
            TreeLinkNode* father=pNode->next;
            if(father->left==pNode)
            {
                return father;
            }
            pNode=father;
        }
        return NULL;
    }
};

 

posted @ 2017-08-01 18:20  双马尾是老公的方向盘  阅读(100)  评论(0编辑  收藏  举报