Binary Tree Max Depth
Given the root node of a binary tree, write a function findMaxDepth
that returns an integer corresponding to the maximum depth.
The maximum depth is going to be the node with the level furthest from the root. The root node is a 0 depth, and an empty input would be considered -1.
The tree nodes will use the following implementation:
Breakdown
Validate My Answer
This can be solved iteratively or recursively using either DFS or BFS. Follow whichever path you feel most comfortable with, but remember to mention that a recursive solution could cause a stack overflow.
All you need to track is the max depth at any point. If the current max you've found is larger than the depth of the current level, then we know to ignore this depth and continue looking. If the current depth is larger than the max we found, we store this as the new max depth.
This can be done in O(n) time and space.
Binary Tree Max Depth
Given the root node of a binary tree, write a function findMaxDepth
that returns an integer corresponding to the maximum depth.
The maximum depth is going to be the node with the level furthest from the root. The root node is a 0 depth, and an empty input would be considered -1.
The tree nodes will use the following implementation: