Create Tree Node
바이너리 트리 생성
class TreeNode {
constructor(val, left, right){
this.val = val;
this.left = left;
this.right = right;
}
}
function createTreeNode (root) {
const rootNode = new TreeNode(root[0])
const treeQueue = [rootNode] ;
let i = 1;
while(treeQueue.length > 0){
const node = treeQueue.shift();
if(i < root.length && root[i] !== null){
node.left = new TreeNode(root[i]);
treeQueue.push(node.left);
}
i++;
if(i < root.length && root[i] !== null){
node.right = new TreeNode(root[i]);
treeQueue.push(node.right);
}
i++;
}
return rootNode;
}
let vals = [1,2,3,null,5];
createTreeNode(vals) ;