Next node binary tree (find successor node in a preorder node)

Copyright Notice: Copyright: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/ysl_ysl123/article/details/90780765

Title Description

And wherein a given binary tree of a node, 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.

Ideas 1

First thought similar topics: binary search tree with a doubly linked list , that is, first order binary tree traversal sequence transfected into doubly linked list (or a single chain), and then returns to the next node to the given node.

Code (c ++)

/*
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:
    void inorder(TreeLinkNode* root,TreeLinkNode* &pre){
        if(root!=NULL){
            inorder(root->left,pre);
            root->left=pre;
            if(pre!=NULL) pre->right=root;
            pre=root;
            inorder(root->right,pre);
        }
    }
    TreeLinkNode* GetNext(TreeLinkNode* pNode)
    {
        if(pNode==NULL) return NULL;
        TreeLinkNode* root=pNode;
        while(root->next!=NULL) root=root->next;
        TreeLinkNode* pre=NULL;
        inorder(root,pre);
        return pNode->right;
    }
};

Ideas 2

According to the concept of the data structure is determined to do.
First go "down" to see that the current node to determine whether there is a right child. If you have the right child, then find the node in the leftmost to the right of the child to the root of the sub-tree, is the successor node of the current node.
If the child is not a right, go down "on" Look, that determine whether the current node is the left child of its parent node, if it is, the parent node is returned; if not, continue to "on" (along parent node point) traversal, traversed until an ancestor node of the current node, which is the left child of its parent node, its parent node is returned, the current node is the successor node. If you can not find an ancestor node of the current node, which is the left child of its parent node, then the description of the current node is the last node in order traversal, returns NULL.

Code (c ++)

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

Guess you like

Origin blog.csdn.net/ysl_ysl123/article/details/90780765