問題形貌
從上往下打印出二叉樹的每一個節點,同層節點從左至右打印。
剖析
二叉樹的條理遍歷,能夠藉助行列的協助
完成
/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
function PrintFromTopToBottom(r)
{
if(r === null)
return [];
var q = [];
var res = [];
q.push(r);
while(q.length !== 0){
var cur = q.shift();
res.push(cur.val);
if(cur.left)
q.push(cur.left);
if(cur.right)
q.push(cur.right);
}
return res;
}