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
만큼 많아질 때 늘어난다.root
를 2^n
만큼 pop
했고, 그때마다 카운트를 기록했다.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;
};