Leetcode 简单二十七 二叉树的最小深度
二叉树的最小深度:
php:
28ms。递归。
/**
* 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(empty($root)){
return 0;
}
$left = $this->minDepth($root->left);
$right = $this->minDepth($root->right);
return ($left && $right) ? min($left,$right)+1: $left+$right+1;
}
}