543-diameter-of-binary-tree

DevGod
Vtuber
/** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } *//** * @param {TreeNode} root * @return {number} */
var diameterOfBinaryTree = function(root) { let diameter = 0
const depth = function(root) { if (!root) return 0; let left = depth(root.left); let right = depth(root.right); diameter = Math.max(diameter, left + right); return Math.max(left, right) + 1; }
depth(root);
return diameter;};