printSpecificLevel_BT
is O(n)
since it looks at every node in the tree. However, with a simply change (returning when level == 0
), you can make it O(min(n, 2^level))
.
getHeight
is O(n)
since it looks at every node in the tree. printBT_LBL
calls getHeight
once and printSpecificLevel_BT
height
times, so its complexity is O(n + n*height) = O(n*height)
.
The space complexity of each is O(height)
since it recurses all the way to the bottom of the tree. You can't say the O(log n)
since the tree isn't necessarily balanced (unless it is).