考虑使用深度优先遍历
    在深度优先遍历的过程中,记录每个节点所在的层级,找出最大层级

    1. const maxDepth = function (root) {
    2. let res = 0
    3. const dfs = (root, l) => {
    4. if(!root) return
    5. console.log(root.val)
    6. if(!root.left && !root.right) res = Math.max(res, l)
    7. dfs(root.left, l+1)
    8. dfs(root.right, l+1)
    9. }
    10. dfs(root, 1)
    11. return res
    12. }