【题目】 104. 二叉树的最大深度
【题目考察】:二叉树
【目标复杂度】: O(n)
【解法一】:递归法
/**
* 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;
};