28. Maximum Depth of Binary Tree
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
To find the maximum depth (height) of a binary tree, I have used the following approach:
I have implemented the Breadth-First Search (BFS) algorithm to traverse all levels of the tree.
I initialize a height
variable to keep track of the number of levels in the tree.
I use a queue (q
) to perform the BFS traversal. I start by pushing the root node into the queue.
While the queue is not empty, I process each level by iterating through the elements in the queue.
For each level, I increment the height
variable by 1.
During each level traversal, I remove the front node from the queue and enqueue its left and right child nodes if they exist.
After processing all the nodes in the current level, I move to the next level by continuing the outer while loop.
Finally, I return the value of the height
variable, which represents the maximum depth (height) of the binary tree.
Time Complexity: O(N)
, where N
is the number of nodes in the binary tree. This is because we visit each node once during the BFS traversal.
Auxiliary Space Complexity: O(M)
, where M
is the maximum number of nodes in a single level of the binary tree. In the worst case, the last level of the tree can contain (N/2)
nodes, where N is the total number of nodes. Therefore, the space required for the queue can be O(N/2)
, which simplifies to O(N)
.
For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the repository.