地址: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; } } }