标签:shift order result control splay font pre 二叉树 one
function levelOrder (root) {
if (!root){
return false;
}
var tree = [];//存放待遍历的节点
var result = [];//存放遍历后的结果
tree.push(root)
while (tree.length){
var node = tree.shift();
result.push(node);
if (node.left) {
tree.push(node.left)
}
if(node.right) {
tree.push(node.right)
}
}
return result;
}
利用了队列先进先出的机制,遍历每一层的节点
标签:shift order result control splay font pre 二叉树 one
原文地址:https://www.cnblogs.com/CoderZX/p/10495002.html