104.Maximum-Depth-of-Binary-Tree
Last updated
Was this helpful?
Last updated
Was this helpful?
Intuition By definition, the maximum depth of a binary tree is the maximum number of steps to reach a leaf node from the root node.
Complexity analysis
Time complexity : we visit each node exactly once, thus the time complexity is O(N), where N is the number of nodes.
Space complexity : in the worst case, the tree is completely unbalanced, e.g. each node has only left child node, the recursion call would occur N times (the height of the tree), therefore the storage to keep the call stack would be O(N). But in the best case (the tree is completely balanced), the height of the tree would be log(N). Therefore, the space complexity in this case would be O(log(N)).