#543. Diameter of Binary Tree

ยท

1 min read

https://leetcode.com/problems/diameter-of-binary-tree/description/?envType=daily-question&envId=2024-02-27


var diameterOfBinaryTree = function(root) {
    let res = 0

    function DFS(node) {
        if (! node) return 0

        let left = DFS(node.left)
        let right = DFS(node.right)

        res = Math.max(res, left + right)

        return 1 + Math.max(left, right)
    }

    DFS(root)
    return res

};
ย