The sequence of binary prove safety offer

Title Description

Implement two functions are used to serialize and deserialize binary

It refers to a sequence of binary: the binary tree is saved in a format string in accordance with the result of a traversing manner such that the memory can be set up binary persisted. Serialization may be based on the first order, in sequence, after, the binary tree traversal sequence is modified, the sequence of the result is a string representing the blank nodes (#) by some sequences of symbol to! End node represents a value (value!).

Deserialized binary tree means: The serialized string str some results obtained traversal order to reconstruct a binary tree.

Thinking

This question is simply extremely sick desperate, probably because I was forced to dish. .
Serialization: according to traverse the level obtained, needless to say
deserialize idea:
Suppose string 12345 # 6 # 7 # # # # # #!!!!!!!!!!!!!! !
corresponding to the structure:
. 1
2. 3
. 4. 5. 6 NULL
NULL NULL NULL NULL NULL. 7
our default serialization algorithm is not NULL subtree, and therefore the third layer is not NULL subtree, only the fourth layer 4, subtree 6.

First, the character string into a queue node
followed recombinant binary

As follows:
First:
q_Tree_2. 1
q_Tree_1 2. 5. 4. 3. 6 NULL NULL NULL NULL NULL NULL. 7

This step will 1pop out one by one to pop out 2 and 3, and arranged to the left and right subtrees 1, 2 and 3 and the pressure in the q_Tree_2

Second:
q_Tree_2 2 3
q_Tree_1 4 5 NULL 6 NULL NULL NULL NULL NULL 7

2 and 3pop out sequentially, the 4,5NULL, 6pop out, it should be noted that the left subtree is NULL 3, does not require a child node assigned to him, you do not need the press-fitting q_Tree_2 NULL

Third:
q_Tree_2 4 5 6
q_Tree_1 NULL NULL NULL NULL NULL 7

Four:
q_Tree_2 7
q_Tree_1

Finish!

Code

class Solution {
public:
    queue<TreeNode*> q_Tree_1;
    queue<TreeNode*> q_Tree_2;
    /* 我这里用了层次遍历,这个就不赘述了,大家应该都知道*/
    char* Serialize(TreeNode *root) {   
        char* res;
        string serial;
        if (root == NULL)
        {
            res = new char[3]();
            res[0] = '#';
            res[1] = '!';
            return res;
        }
        queue<TreeNode*> q_Tree;
        q_Tree.push(root);
        while (q_Tree.size() > 0)
        {
            TreeNode* current = q_Tree.front();
            q_Tree.pop();
            if (current == NULL)
            {
                serial = serial + '#' + '!';
            }
            else
            {
                serial = serial + to_string(current->val) + '!';
                if (current->left == NULL && current->right == NULL)
                {
                    continue;
                }
                else
                {
                    q_Tree.push(current->left);
                    q_Tree.push(current->right);
                }

            }
        }
        int a = serial.size();
        char* res_2= new char[a + 1]();
        memcpy(res_2, serial.c_str(), a);
        return res_2;
    }
    /*  这个是逆序列函数,将一段字符串换成一堆结点保存到q_Tree_1中 */
    TreeNode* Deserialize(char *str) {
        string s = str;
        if (s.size() == 0 || s[0] == '#')
        {
            return NULL;
        }
        int end = 0;
        while (end < s.size())
        {
            TreeNode* node = sring_node(s, end, s.find("!", end + 1));
            q_Tree_1.push(node);
            if (end == s.size())
                break;
            else
                end = s.find("!", end + 1) + 1;
        }
        TreeNode* root;
        root = Deserialize_sub();
        return root;
    }
    /* 这个函数是将一段字符转换为数字,start标志上一个!的下一位,end表示这次找到的!,二者就将这个结点数字对应的字符取出*/
    TreeNode* sring_node(string s,int start, int end)
    {
        int number = 0;
        for (int i = start; i < end; i++)
        {
            if (s[i] == '#')
            {
                return NULL;
            }
            else
            {
                number = number + (s[i] - '0')*pow(10, end - i - 1);
            }
        }
        TreeNode* node = new TreeNode(number);
        return node;
    }
        /* 这个函数是将结点重构成树,利用两个队列,队列1保存剩余结点,队列2保存下次分配子结点的那些结点。特别需要注意的!调了2个小时才明白的!每次队列1pop之后,需要判断pop出来的是不是NULL,NULL的就不push到队列2中,因为NULL没有子树*/
    TreeNode* Deserialize_sub()
    {
        if (q_Tree_1.size() == 0)
		    return NULL;
        int count = 0;
        TreeNode* root;
        root = q_Tree_1.front();
        q_Tree_1.pop();
        q_Tree_2.push(root);
        while (q_Tree_1.size()>0)
        {
            int size = q_Tree_2.size();
            for (int i = 0; i < size; i++)
            {
                TreeNode* node = q_Tree_2.front();
                q_Tree_2.pop();
                TreeNode* node_left = q_Tree_1.front();
                q_Tree_1.pop();
                node->left = node_left;
                TreeNode* node_right = q_Tree_1.front();
                q_Tree_1.pop();
                node->right = node_right;
                if(node_left) q_Tree_2.push(node_left);
                if(node_right) q_Tree_2.push(node_right);
            }
            count++;
        }
            return root;
        }
};
Published 85 original articles · won praise 0 · Views 395

Guess you like

Origin blog.csdn.net/weixin_38312163/article/details/104853889