<?php
class Nexus
{
public $parent;
public $leftChild;
public $rightChild;
public function reverseChilds()
{
$temp = $this->leftChild;
$this->leftChild = $this->rightChild;
$this->rightChild = $temp;
}
public function createChilds()
{
$this->leftChild = new Nexus($this);
$this->rightChild = new Nexus($this);
}
public function __construct($parent = null)
{
$this->parent = $parent;
}
}
function buildATree(Nexus $base, $depth = 3, $i = 0)
{
$base->createChilds();
if ($i < $depth) {
$i++;
$base->leftChild = buildATree($base->leftChild, $depth, $i);
$base->rightChild = buildATree($base->rightChild, $depth, $i);
return $base;
}
}
function reverseAllChildsInTree(Nexus $base)
{
if (!empty($base->leftChild) && !empty($base->rightChild)) { $base->reverseChilds();
$base->leftChild = reverseAllChildsInTree($base->leftChild);
$base->rightChild = reverseAllChildsInTree($base->rightChild);
}
return $base;
}
$topNexus = new Nexus();
$topNexus = buildATree($topNexus);
//var_dump($topNexus);
$topNexus = reverseAllChildsInTree($topNexus);
PD9waHAKZXJyb3JfcmVwb3J0aW5nKC0xKTsKIApjbGFzcyBOZXh1cwp7CglwdWJsaWMgJHBhcmVudDsKCXB1YmxpYyAkbGVmdENoaWxkOwoJcHVibGljICRyaWdodENoaWxkOwogCglwdWJsaWMgZnVuY3Rpb24gcmV2ZXJzZUNoaWxkcygpCgl7CgkJJHRlbXAgPSAkdGhpcy0+bGVmdENoaWxkOwoJCSR0aGlzLT5sZWZ0Q2hpbGQgPSAkdGhpcy0+cmlnaHRDaGlsZDsKCQkkdGhpcy0+cmlnaHRDaGlsZCA9ICR0ZW1wOwoJfQogCiAgICBwdWJsaWMgZnVuY3Rpb24gY3JlYXRlQ2hpbGRzKCkKICAgIHsKICAgICAgICAkdGhpcy0+bGVmdENoaWxkID0gbmV3IE5leHVzKCR0aGlzKTsKICAgICAgICAkdGhpcy0+cmlnaHRDaGlsZCA9IG5ldyBOZXh1cygkdGhpcyk7CiAgICB9CiAKCXB1YmxpYyBmdW5jdGlvbiBfX2NvbnN0cnVjdCgkcGFyZW50ID0gbnVsbCkKCXs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