Skip to main content

104.js

/** ------------------------------------------------------------------------------
*
* 104. Maximum Depth of Binary Tree
* Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree
* https://leetcode.com/problems/maximum-depth-of-binary-tree/description/
*
------------------------------------------------------------------------------ */
/**
* 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}
*/
var maxDepth = function (root) {
if (!root) return 0
const left = maxDepth(root.left)
const right = maxDepth(root.right)

return Math.max(left, right) + 1
}

console.log(maxDepth([3, 9, 20, null, null, 15, 7]))