hackerrank php第22天二进制搜索树

hackerrank php第22天二进制搜索树,php,binary-search-tree,Php,Binary Search Tree,我有以下代码: <?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)

我有以下代码:

<?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\n";
        echo "heightLeft is $heightLeft\n";

        $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;
?>

当我用输入运行它时 1. 1. 它给出了正确的结果

但当我用输入运行它时 2. 1. 二,

我得到一个错误:

PHP致命错误:在第36行的C:\git\phpStudy\CallingAFunction.PHP中调用未定义的函数getHeight()

致命错误:在第36行的C:\git\phpStudy\CallingAFunction.php中调用未定义的函数getHeight()


我是php新手,不知道自己做错了什么。谢谢。

答案很简单。简而言之,您的问题是:

a) 导致如下所述的致命错误:
class Solution{
    public function getHeight($a) {
        if($a==true) {
            return getHeight(false);
        }
        return "hello";
    }
}

$a = new Solution();

echo $a->getHeight(true);
b) 作品: 如果要调用类内的函数,则需要引用该类。使用
$this->

在第36行中,您有一个递归函数调用来获取高度。找不到该函数。因此,正确的解决方案是:

<?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\n";
        echo "heightLeft is $heightLeft\n";

        $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;
?>

答案很简单。简而言之,您的问题是:

a) 导致如下所述的致命错误:
class Solution{
    public function getHeight($a) {
        if($a==true) {
            return getHeight(false);
        }
        return "hello";
    }
}

$a = new Solution();

echo $a->getHeight(true);
b) 作品: 如果要调用类内的函数,则需要引用该类。使用
$this->

在第36行中,您有一个递归函数调用来获取高度。找不到该函数。因此,正确的解决方案是:

<?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\n";
        echo "heightLeft is $heightLeft\n";

        $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;
?>


非常感谢您。我不熟悉在php中使用类的语法。这很有道理,效果也很好。没问题;-)请将问题标记为已回答:-)我不敢相信我所需要的只是一点点钱这个->。。。很高兴我问了,否则的话,我可能还要挠头好几天了。唉,太谢谢你了。我不熟悉在php中使用类的语法。这很有道理,效果也很好。没问题;-)请将问题标记为已回答:-)我不敢相信我所需要的只是一点点钱这个->。。。很高兴我问了,否则的话,我可能还要挠头好几天了。唉。