[Algorithm] 24 week(6.27 ~ 7.03) 3/3

Dev_min·2022년 7월 3일
0

algorithm

목록 보기
77/157

543. Diameter of Binary Tree

var diameterOfBinaryTree = function(root) {
    let max = 0;
    
    const dfs = (node) => {
        if (!node) return -1
        let left = dfs(node.left);
        let right = dfs(node.right);
        max = Math.max(max, 2 + left + right);
        return Math.max(left, right) + 1
    }
    
    dfs(root);
    
    return max;    
};
profile
TIL record

0개의 댓글