@ 2018-06-19T14:47:03Z <?php
// Tree data structure
class BinaryNode
{
public $value = null; // node value
public $left = null; // left child
public $right = null; // right child
public function __construct($value) {
$this->value = $value;
}
}
/**
* invertTree function goes here
*/
function invertTree($node)
{
if($node == null){
return null;
}
$left = invertTree($node->left);
$right = invertTree($node->right);
$node->left = $right;
$node->right = $left;
return $node;
}
$root = new BinaryNode(1);
$rootLeftChild = new BinaryNode(2);
$rootRightChild = new BinaryNode(3);
$rootLeftChildLeftChild = new BinaryNode(4);
$rootLeftChildRightNode = new BinaryNode(5);
$rootRightChildLeftChild = new BinaryNode(6);
$rootRightChildRightNode = new BinaryNode(7);
$rootLeftChild->left = $rootLeftChildLeftChild;
$rootLeftChild->right = $rootLeftChildRightNode;
$rootRightChild->left = $rootRightChildLeftChild;
$rootRightChild->right = $rootRightChildRightNode;
$root->left = $rootLeftChild;
$root->right = $rootRightChild;
$invertedTree = invertTree($root);
var_dump($invertedTree->value == 1);
var_dump($invertedTree->left->value == 3);
var_dump($invertedTree->right->value == 2);
var_dump($invertedTree->left->left->value == 7);
var_dump($invertedTree->left->right->value == 6);
var_dump($invertedTree->right->left->value == 5);
var_dump($invertedTree->right->right->value == 4);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 5.6.0 - 5.6.40 , 7.0.0 - 7.0.33 , 7.1.0 - 7.1.33 , 7.2.0 - 7.2.33 , 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.4 , 8.3.6 bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
Output for 8.3.5 Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
preferences:dark mode live preview
201.19 ms | 402 KiB | 287 Q