Created
August 30, 2016 00:35
-
-
Save kinncj/a19ab9def388ff85eff04ac0c5a3e7f2 to your computer and use it in GitHub Desktop.
binaryTree max height PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Node | |
{ | |
private $left; | |
private $right; | |
public function __construct(Node $left = null, Node $right = null) | |
{ | |
$this->left = $left; | |
$this->right = $right; | |
} | |
public function __get($attr) | |
{ | |
return $this->{$attr}; | |
} | |
} | |
function maxHeight(Node $node = null) | |
{ | |
if (!$node) { | |
return 0; | |
} | |
$l = maxHeight($node->left); | |
$r = maxHeight($node->right); | |
return ($l > $r) ? $l +1 : $r + 1; | |
} | |
$node = new Node( | |
new Node( | |
new Node( | |
new Node( | |
new Node(), | |
new Node() | |
), | |
new Node() | |
), | |
new Node() | |
), | |
new Node( | |
new Node( | |
new Node() | |
), | |
new Node() | |
) | |
); | |
var_dump(maxHeight($node)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment