hackerrank php第22天二叉搜索树

I have the following code:

<?php
class Node{
    public $left,$right;
    public $data;
    function __construct($data)
    {
        $this->left=$this->right=null;
        $this->data = $data;
    }
}
class Solution{
    public function insert($root,$data){
        if($root==null){
            return new Node($data);
        }
        else{
            if($data<=$root->data){
                $cur=$this->insert($root->left,$data);
                $root->left=$cur;
            }
            else{
                $cur=$this->insert($root->right,$data);
                $root->right=$cur;
            }
            return $root;
        }
    }
    public function getHeight($root) {
        $heightLeft = 0;
        $heightRight = 0;

        if ($root->left != null) {
            $heightLeft = getHeight($root->left) + 1;
        }
        if ($root->right != null) {
            $heightRight = getHeight($root->right) + 1;
        }
        echo "heightRigh is $heightRight
";
        echo "heightLeft is $heightLeft
";

        $ans = ($heightLeft > $heightRight ? $heightLeft : $heightRight);
        return $ans;
    }

}//End of Solution
$myTree=new Solution();
$root=null;
$T=intval(fgets(STDIN));
while($T-->0){
    $data=intval(fgets(STDIN));
    $root=$myTree->insert($root,$data);
}
$height=$myTree->getHeight($root);
echo $height;
?>

When I run it with the inputs 1 1 it gives the correct results.

But when I run it with the inputs 2 1 2

I get the error:

PHP Fatal error: Call to undefined function getHeight() in C:\git\phpStudy\CallingAFunction.php on line 36

Fatal error: Call to undefined function getHeight() in C:\git\phpStudy\CallingAFunction.php on line 36

I am new to php and can't figure out what I am doing wrong. Thank you.

The answer is very easy. In short your problem is this:

a) leads to fatal error as described:

class Solution{
    public function getHeight($a) {
        if($a==true) {
            return getHeight(false);
        }
        return "hello";
    }
}

$a = new Solution();

echo $a->getHeight(true);

b) works:

class Solution{
    public function getHeight($a) {
        if($a==true) {
            return $this->getHeight(false);
        }
        return "hello";
    }
}

$a = new Solution();

echo $a->getHeight(true);

You need to reference to the class if you want to call a function inside the class. Use $this->.

In line 36 you have a recursive function call to get height. The function is not found. Correct solution is therefore:

<?php
class Node{
    public $left,$right;
    public $data;
    function __construct($data)
    {
        $this->left=$this->right=null;
        $this->data = $data;
    }
}
class Solution{
    public function insert($root,$data){
        if($root==null){
            return new Node($data);
        }
        else{
            if($data<=$root->data){
                $cur=$this->insert($root->left,$data);
                $root->left=$cur;
            }
            else{
                $cur=$this->insert($root->right,$data);
                $root->right=$cur;
            }
            return $root;
        }
    }
    public function getHeight($root) {
        $heightLeft = 0;
        $heightRight = 0;

        if ($root->left != null) {
            $heightLeft = $this->getHeight($root->left) + 1;
        }
        if ($root->right != null) {
            $heightRight = $this->getHeight($root->right) + 1;
        }
        echo "heightRigh is $heightRight
";
        echo "heightLeft is $heightLeft
";

        $ans = ($heightLeft > $heightRight ? $heightLeft : $heightRight);
        return $ans;
    }

}//End of Solution
$myTree=new Solution();
$root=null;
$T=intval(fgets(STDIN));
while($T-->0){
    $data=intval(fgets(STDIN));
    $root=$myTree->insert($root,$data);
}
$height=$myTree->getHeight($root);
echo $height;
?>