4

他の $branch オブジェクトを含むことができる $branch オブジェクトを取得しました。

$branch->children(); 

それぞれが子として $apples を持つことができます。

$branch->apples();

$branch からすべての $apples を再帰的に収集するにはどうすればよいですか?

function collectApples($branch){
    $apples = array();
    ?
    return $apples;
}
4

2 に答える 2

5

DFSを使用して特定のブランチのリンゴをすべて収集します。

function collectApples($branch) {
    $apples = $branch->apples();
    foreach ($branch->children() as $child) {
        $apples = array_merge($apples, collectApples($child));
    }
    return $apples;
}
于 2013-04-19T15:49:46.680 に答える