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.

图片转代码服务由CSDN问答提供 功能建议

我有以下代码:

 &lt;?php  
class Node {
 public $ left,$ right; 
 public $ data; 
 function __construct($ data)
 {
 $ this-&gt; left = $ this-&gt; right = null; 
  $ this-&gt; data = $ data; 
} 
} 
class解决方案{
公共函数insert($ root,$ data){
 if($ root == null){
 return new Node(  $ data); 
} 
 else {
 if($ data&lt; = $ root-&gt; data){
 $ cur = $ this-&gt; insert($ root-&gt; left,$ data);  
 $ root-&gt; left = $ cur; 
} 
 else {
 $ cur = $ this-&gt; insert($ root-&gt; right,$ data); 
 $ root-&gt; right  = $ cur; 
} 
返回$ root; 
} 
} 
公共函数getHeight($ root){
 $ heightLeft = 0; 
 $ heightRight = 0; 
 
 if($)  root-&gt; left!= null){
 $ heightLeft = getHeight($ root-&gt; left)+ 1; 
} 
 if($ root-  &gt; right!= null){
 $ heightRight = getHeight($ root-&gt; right)+ 1; 
} 
 echo“heightRigh is $ heightRight 
”; 
 echo“heightLeft is $ heightLeft 
  “; 
 
 $ ans =($ heightLeft&gt;  $ heightRight?  $ heightLeft:$ heightRight); 
返回$ ans; 
} 
 
} //解决方案结束
 $ myTree = new Solution(); 
 $ root = null; 
 $ T = intval(  fgets(STDIN)); 
while($ T  - &gt; 0){
 $ data = intval(fgets(STDIN)); 
 $ root = $ myTree-&gt; insert($ root,$ data);  
} 
 $ height = $ myTree-&gt; getHeight($ root); 
echo $ height; 
?&gt; 
   
 
 

当我运行它时 使用输入 1 1 it给出正确的结果。

但是当我使用输入 2 1 2

运行它时 我收到错误:

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

我是php新手,无法弄清楚我是什么 我做错了。 谢谢。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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;
    ?>
    
    点赞 打赏 评论

相关推荐 更多相似问题