题目:力扣
思路:深度优先遍历,用一个变量记录当前的最大深度
代码:
var maxDepth = function (root) {
if (!root) return 0
let l = 0
function getMaxL(root, deep) {
l = Math.max(l, deep)
if (root.left) {
getMaxL(root.left, deep + 1)
}
if (root.right) {
getMaxL(root.right, deep + 1)
}
}
getMaxL(root, 1)
return l
};
结果: