考虑使用深度优先遍历
在深度优先遍历的过程中,记录每个节点所在的层级,找出最大层级
const maxDepth = function (root) {
let res = 0
const dfs = (root, l) => {
if(!root) return
console.log(root.val)
if(!root.left && !root.right) res = Math.max(res, l)
dfs(root.left, l+1)
dfs(root.right, l+1)
}
dfs(root, 1)
return res
}