111. 二叉树的最小深度

地址:https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/

/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     public $val = null;
 *     public $left = null;
 *     public $right = null;
 *     function __construct($value) { $this->val = $value; }
 * }
 */
class Solution {

    /**
     * @param TreeNode $root
     * @return Integer
     */
    function minDepth($root) {
        if($root == null){
            return 0;
        }elseif($root->left == null || $root->right == null){
            return $this->minDepth($root->left) + $this->minDepth($root->right) +1;
        }else{
            $l = $this->minDepth($root->left);
            $r = $this->minDepth($root->right);
            return $l<$r ? $l+1:$r+1;
        }
    }
}

 

posted @ 2020-05-21 09:10  花花妹子。  阅读(113)  评论(0)    收藏  举报