const TreeNode = {
val: 1,
left: {
val: 2,
left: {
val: 4,
},
right: {
val: 5
}
},
right: {
val: 3,
left: {
val: 6,
},
right: {
val: 7
}
}
};
const levelOrder = (root) => {
const queue = [];
const valArr = [];
if(root === null){
return;
}
queue.push(root);
while(queue.length !== 0){
const cur = queue.pop();
valArr.push(cur.val);
if(cur.left){
queue.unshift(cur.left);
}
if(cur.right){
queue.unshift(cur.right);
}
}
return valArr;
};
console.log(levelOrder(TreeNode));
网友评论