104. Maximum Depth of Binary Tree

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

    /**
     * @param TreeNode $root
     * @return Integer
     */
    function maxDepth($root) {
        if ($root === null){
            return 0;
        }
        // 每次都向左向右走,並回傳最大深度,最後左右節點深度比對;
        $left = 1 + $this->maxDepth($root->left);
        $right = 1 + $this->maxDepth($root->right);
        
        return max($left, $right);
    }
}

Last updated