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;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章