Here is the interview question prompt, presented for reference.
Write a method to determine how deep a binary tree
goes. The tree's depth
can be described as the number of nodes you encounter as you traverse from the root
node to a leaf
.
The root
node is the topmost node, and a leaf
is a node with no children.
For example, you're given the following binary tree:
6
/ \
2 14
/ \
13 19
The longest distance is from 6 to 19, and we return 3
because it covers 3 nodes.
The method will be invoked like the following:
const root = new Node(6);
function howDeep(root) {
return;
};
howDeep(root);
100000
-1000000000
and 1000000000
O(logn)
O(n)
considering the call stackYou can see the full challenge with visuals at this link.
Challenges • Asked almost 7 years ago by Jake from AlgoDaily
This is the main discussion thread generated for How Deep Does It Go? Maximum Depth of Binary tree.