1
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
// https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/ 104
// 二叉树最大深度
const maxDepth = function(root) {
if (!root) {
return 0
}
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1
}
// https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/ 111
// 二叉树最小深度
const minDepth = function(root) {
if (!root) {
return 0
}
const left = minDepth(root.left)
const right = minDepth(root.right)
return (Math.min(left, right) || Math.max(left, right)) + 1
}
1