104.maximum-depth-of-binary-tree
Last updated
Last updated
Input: root = [3,9,20,null,null,15,7]
Output: 3Input: root = [1,null,2]
Output: 2/**
* 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) {
let max = 0;
const traversal = (node, count = 0) => {
if (node === null) return;
count++;
if (count > max) max = count;
if (node?.left) traversal(node.left, count);
if (node?.right) traversal(node.right, count);
};
traversal(root);
return max;
};