du548397507 2017-02-10 17:33
浏览 48
已采纳

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.

  • 写回答

1条回答 默认 最新

  • dsw8292301 2017-02-10 17:46
    关注

    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;
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮