104. 二叉树的最大深度

var maxDepth = function (root) {
  if (!root) return 0;
  let res = 0;
  const dfs = (root, l) => {
    if (!root) return;
    if (!root.left && !root.right) {
      res = Math.max(l, res);
    }
    dfs(root.left, l + 1);
    dfs(root.right, l + 1);
  };
  dfs(root, 1);
  return res;
};

 

posted @ 2021-07-26 16:38  jlin7  阅读(30)  评论(0)    收藏  举报