从上往下打印出二叉树的每个节点,同层节点从左至右打印。
/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
function PrintFromTopToBottom(root)
{
// write code here
if(root == null) return [];
var tree = [], res = [];
tree.push(root);
while(tree.length){
var temp = tree.shift();
res.push(temp.val)
if(temp.left){
tree.push(temp.left)
};
if(temp.right){
tree.push(temp.right);
}
}
return res;
}