How do you determine the maximum depth of a binary tree?
LeetCode Patterns Flashcards: Trees, DFS, BFS, Traversals, BST Properties
Аудио-карточка · 0:30Nortren·
How do you determine the maximum depth of a binary tree?
0:30
Use DFS recursion: the maximum depth of a node is one plus the maximum of the depths of its left and right children. The base case is a null node with depth zero. This naturally computes the answer bottom-up in O of n time and O of h space on the call stack. Alternatively, use BFS and count the number of levels processed, which also gives the maximum depth in O of n time. Maximum depth is equivalent to height for the root node. This simple recursive pattern is the foundation for more complex tree problems like diameter, balanced tree check, and subtree problems.
neetcode.io