方式1:
class Node {
// 定义节点
constructor(data){
this.data = data
this.leftChild = null
this.rightChild = null
}
}
const createTree = (arr) => {
// 创建二叉树
let tree = new Node(arr[0])
let Nodes = [tree]
let i = 1
for (let node of Nodes){
Nodes.push(node.leftChild = new Node (arr[i]))
i += 1
if (i == arr.length) return tree
Nodes