HR30 Day 22: Binary Search Trees/PHP

개요[ | ]

<?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){
        //Write your code here
        if( is_null($root) ) return -1;
        $left = $this->getHeight($root->left);
        $right = $this->getHeight($root->right);
        return max($left, $right)+1;
    }
}//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;
?>
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}