Answer the question
In order to leave comments, you need to log in
How to get the dates of all child objects of a binary tree?
here is the tree
Dump
I try like this (getChil is already a piece of the tree if it is present in the tree with the given number.)
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;
}
Answer the question
In order to leave comments, you need to log in
You have the correct code:
sandbox.onlinephpfunctions.com/code/00ed97cf62c568...
<?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"
// ]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question