public function getChil($node)
{
if (!empty($node->data)){
array_push($this->arr, $node->data);
}
if (!empty($node->left)){
$this-> getChil($node->left);
}
if (!empty($node->right)){
$this-> getChil($node->right);
}
return $this->arr;
}
<?php
class Node{
private $arr = [];
public function getChildren($node)
{
if (!empty($node->data)){
array_push($this->arr, $node->data);
}
if (!empty($node->left)){
$this->getChildren($node->left);
}
if (!empty($node->right)){
$this->getChildren($node->right);
}
return $this->arr;
}
}
$node = createNode(
6 . '_base',
createNode(7 . '_right'),
createNode(8 . '_left',
createNode(81 . '_left'),
createNode(82 . '_right',
createNode(821 . '_right',
null,
createNode(824 . '_left')
)
)
)
);
function createNode($data, $right = null, $left = null) {
$obj = new stdClass();
$obj->data = $data;
$obj->right = $right;
$obj->left = $left;
return $obj;
}
var_dump((new Node)->getChildren($node));
// [
// "6_base"
// "8_left"
// "82_right"
// "821_right"
// "824_left"
// "81_left"
// "7_right"
// ]