Skip to content

Instantly share code, notes, and snippets.

@kinncj
Created August 30, 2016 00:35
Show Gist options
  • Save kinncj/a19ab9def388ff85eff04ac0c5a3e7f2 to your computer and use it in GitHub Desktop.
Save kinncj/a19ab9def388ff85eff04ac0c5a3e7f2 to your computer and use it in GitHub Desktop.
binaryTree max height PHP
<?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