543.二叉树的直径

var diameterOfBinaryTree = function (root) {
  let Max = 0;
  const depth = (root) => {
    if (!root) return 0;
    let l = depth(root.left);
    let r = depth(root.right);
    if (l + r > Max) {
      Max = l + r;
    }
    return Math.max(l, r) + 1;
  };
  depth(root);
  return Max;
};

 

posted @ 2021-07-27 13:48  jlin7  阅读(29)  评论(0)    收藏  举报