LC - Max Depth Of Binary Tree[실패]

Goody·2021년 2월 23일
0

알고리즘

목록 보기
50/122

문제

Given the root of a binary tree, return its maximum depth.

A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.


예시

Input: root = [3,9,20,null,null,15,7]
Output: 3

Input: root = [1,null,2]
Output: 2


풀이

  • 이진 트리의 깊이는 배열의 원소가 2^n 만큼 많아질 때 늘어난다.
  • 따라서 주어진 root2^n 만큼 pop 했고, 그때마다 카운트를 기록했다.
  • vscode에서는 분명 잘 출력되는데, 릿코드 제출페이지 에서는 통과가 되지 않았다.

코드

var maxDepth = function(root) {
    const rootCopy = root;
    let depth = 0;
    let n = 0;

while(rootCopy.length > 0) {
    for(let i = 0; i < 2**n; i++) {
        rootCopy.pop();
    }
    depth++;
    n++;
}

    return depth;

};

0개의 댓글