JS构建二叉树
//定义节点类
class Node{
constructor(value){
this.val = value; //存放值
this.left = null; //左节点指针
this.right = null; //右节点指针
}
}
//创建二叉树函数
function createBinaryTree(){
const root = new Node(1); //创建根节点
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4)
root.left.right = new Node(5)
return root
}
const binaryTree = createBinaryTree();
console.log(binaryTree);