9. Height of Binary Tree
The problem can be found at the following link: Question Link
My Approach
To determine the height of a binary tree, I used a recursive approach.
Starting from the root node, I calculate the height of the left and right subtrees recursively and return the maximum of the two heights, plus one.
Time and Auxiliary Space Complexity
Time Complexity: The time complexity of this solution is
O(n)
, wheren
is the number of nodes in the binary tree. This is because I visit each node once, and the work done at each node is constant.Auxiliary Space Complexity: The auxiliary space complexity is
O(h)
, whereh
is the height of the binary tree. This space is used for the recursive call stack. In the worst case, for a skewed tree, the space complexity isO(n)
, but for a balanced tree, it isO(log n)
.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. 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 getlost01/gfg-potd repository.
Last updated