The sword refers to offer (C++)-JZ8: the next node of the binary tree (data structure - tree)

Author: Steven Zhai
Copyright Notice: The copyright belongs to the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source

Topic description:

Given a node in a binary tree, find the next node in inorder traversal order and return. Note that the nodes in the tree contain not only left and right child nodes, but also the next pointer to the parent node. The figure below shows a binary tree with 9 nodes. The pointer from the parent node to the child node in the tree is represented by a solid line, and the pointer from the child node to the parent node is represented by a dashed line

Example:

Input: {8,6,10,5,7,9,11},8

Returns: 9

Analysis: The root node of the subtree passed in by this assembly is actually the entire tree. In-order traversal {5,6,7,8,9,10,11}, the next node of the root node 8 is 9, which should return { 9,10,11}, the background only prints the next node of the subtree, so only 9 is printed, as shown in the figure below, in fact, there are pointers to the left and right children, as well as pointers to the parent node, which are not drawn in the figure below

Data range: the number of nodes satisfies 1≤n≤50, and the value on the node satisfies 1≤val≤100 

requirements: space complexity O(1), time complexity O(n) 

Example:

enter:

{8,6,10,5,7,9,11},8

return value:

9

Problem solving ideas:

This question examines the use of data structure trees. Two methods:

1) Brute force cracking. Obtain the root node through the next pointer, sort it in the middle order, store it in vector during the sorting process, and then output it directly according to the position.

2) Combined with the properties of in-order sorting. If a node has a right subtree, the leftmost child of the right subtree is its next node; if there is no right subtree, its first right parent is its next node.

Test code:

1) Brute force cracking.

/*
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)
            return NULL;
        // 确定根结点
        TreeLinkNode* root=pNode;
        while(root->next)
        {
            root=root->next;
        }
        // 中序排序
        vector<TreeLinkNode*> v;
        inorder(root,v);
        for(int i=0;i<v.size();++i)
        {
            if(v[i]==pNode&&(i+1)<v.size())
                return v[i+1];
        }
        return NULL;
    }
    
    // 排序
    void inorder(TreeLinkNode* root,vector<TreeLinkNode*> &v)
    {
        if(!root)
            return;
        // 中序排序
        inorder(root->left,v);
        v.push_back(root);
        inorder(root->right,v);
    }
};

2) Combined with the properties of in-order sorting.

/*
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)
            return NULL;
        // 判断是否存在右子树
        if(pNode->right)
        {
            TreeLinkNode* target=pNode->right;
            // 取最左孩子
            while(target->left)
            {
                target=target->left;
            }
            return target;
        }
        // 不存在右子树,寻找第一个右父亲
        while(pNode->next)
        {
            if(pNode->next->left==pNode)
                return pNode->next;
            pNode=pNode->next;
        }
        return NULL;
    }
    

};

Guess you like

Origin blog.csdn.net/zhaitianbao/article/details/123876984