Mark As Completed Discussion

One Pager Cheat Sheet

  • Write a method to calculate the depth of a binary tree using O(logn) time complexity and O(n) space complexity.
  • We can traverse the binary tree to count its maximum levels by navigating two children at each node and detecting a leaf when a left or right does not exist.
  • We can return when there is no node present.
  • We can recursively traverse down the left and right children of each node until we reach a leaf, returning the number of levels traversed and with a time complexity of O(log n).

This is our final solution.

To visualize the solution and step through the below code, click Visualize the Solution on the right-side menu or the VISUALIZE button in Interactive Mode.

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Great job getting through this. Let's move on.

If you had any problems with this tutorial, check out the main forum thread here.