Wins the next node in the binary tree offer-08

Question 8 wins the offer, have been looking for the corresponding leetcode title, then did not find the cattle off the Internet directly to the brush.

Subject description:

And wherein a given binary tree of a node (pNode), please find the next node in a preorder traversal order and returns. Note that the node in the tree contains not only the left and right child nodes, the parent node contains a pointer pointing to.

analysis

I saw the first idea of solving the problem, that is, do not control the left subtree. Because in order traversal, the left subtree before visited, so the next node only two possibilities.
First, in the right subtree
Second, part of the parent node
with different situations:
First, in the right subtree
if only a right child node that better said, the next node is the right child. But if the right sub-tree is a tree, then we have to look at the most left-right child node of the tree.

        TreeLinkNode * point=pNode->right;
        while(point->left!=NULL)
        {
            point=point->left;
        }
        return point;

Second, over the parent node
if the right subtree is empty, then two cases.
(1) pNode is the left child node parent node, that is just right, the next to be visited is the father node directly back father node on the line;
(2) pNode is the right child parent node, indicating that the next area where the node, also at least on the parent node parent node.
I started around in a distant second case, I am thinking of using recursive resolved. If pNode father is right child node, then pNode = pNode-> next; while his father cut off the right child node of the tree. This construct a sub-issue, the problem converted after a given node father, looking for problems to access the next node.

        if(pNode->right==NULL)
        {
            if(pNode->next==NULL||pNode==pNode->next->left)
                return pNode->next;
            else{
                pNode->next->right=NULL;
                return GetNext(pNode->next);
            }
        }

Say a little about the whole idea is to cut off the right subtree, so a pointer to the father, then the new tree, looking for the next node pointer to the node to be accessed. In fact, I personally feel quite clever.
But still to complicate the simple question, but in fact has a direct line to the father looking until you find satisfying situation pNode (1) returns pNode-> next.

        if(pNode->right==NULL)
        {
            while(pNode->next!=NULL&&pNode!=pNode->next->left)
                pNode=pNode->next;
            return pNode->next;
        }

Finally put about the whole code is:

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

Guess you like

Origin www.cnblogs.com/HaoPengZhang/p/11545424.html