demand

  • Enter an array of binary trees and the function generates the corresponding binary tree structure

code

const deserialize = (data) = > {

    let res = ' '
    for (let v of data) {
        res = res + v + ', ';
    }
    for (let i = 0; i <= data.length; i++) {
        res = res + 'null' + ', '
    }
    data = res;

    function TreeNode(val) {
        this.val = val;
        this.left = null;
        this.right = null;
    }
    if (data.length === 0) return null;
    const list = data.split(', ');   // split into an array
    list.splice(list.length - 1);
    let list_Pointer = 1;
    let queue_pointer = 0;
    const root = new TreeNode(list[0])
    let queue = [root];

    while(list_Pointer ! == list.length) {if (queue[0= = =null) {
            queue.shift();
            queue_pointer++;
            continue;
        }
        if (queue_pointer === list_Pointer) {
            list_Pointer = list_Pointer + 2;
        }
        if (list[list_Pointer] === 'null') {
            queue[0].left = null;
        } else {
            queue[0].left = new TreeNode(list[list_Pointer]);
        }
        if (list[list_Pointer + 1= = ='null') {
            queue[0].right = null;
        } else {
            queue[0].right = new TreeNode(list[list_Pointer + 1]);
        }
        queue.push(queue[0].left);
        queue.push(queue[0].right);
        queue.shift();
        queue_pointer++
        list_Pointer = list_Pointer + 2;
    }
    return root
};

const test2 = deserialize([1.2.3.4]);
Copy the code

Matters needing attention

The val value of each node in the binary tree is not a Number, but a string