题目描述
从上往下打印出二叉树的每个节点,同层节点从左至右打印。
<?php
/*class TreeNode{
var $val;
var $left = NULL;
var $right = NULL;
function __construct($val){
$this->val = $val;
}
}*/
function PrintFromTopToBottom($root)
{
$arr=[];
$temp=[];
$res=[];
if($root==null)
return $res;
array_push($arr,$root);
while(count($arr)>0){
$temp=array_shift($arr);
if($temp->left)
array_push($arr,$temp->left);
if($temp->right)
array_push($arr,$temp->right);
//array_push($res,$temp->val);
$res[]=$temp->val;
}
return $res;
}