Find Height of a Binary Tree (Algo + Code + Complexity)

preview_player
Показать описание
This video will show you how to find the height of a binary tree.

We will first go through the algorithm, then write the code, and finally, analyze the time and space complexity.

What is the height of a binary tree?

The height of a binary tree is the largest number of edges in a path from the root node to a leaf node.

How can we find it?

The algorithm to find the height is very simple: we start at the root node and find the height of the left subtree, then the height of the right subtree, and finally, we return the maximum of the two heights plus one.

Рекомендации по теме
Комментарии
Автор

Thank you for taking the time to do this. Man, I've spent days looking for something like this video. This video explains in detail what's happening behind the scenes with recursion in trees. It takes patience, focus, paper and pencil to fully understand. 5/5

gasparstudios
Автор

Yo this video was so detailed and well explained. Looked so long for a video that covers the algorithm extensively, and you do an amazing job! Thanks a ton.

varunmodak
Автор

Yo, your video is amazing. Like I couldn't move on without visualizing what you shared in this video. Thanks and I am looking forward to equivalent content in other parts of DSA.

emmanuelezeka
Автор

Most helpful video yet, still not clicked with me yet but I feel about 10 steps further now

chestermartin
Автор

Very helpful thank you, I was conceptually stuck trying to figure out how this worked.

AudiAR
Автор

The best video on youtube about it. Thank you

gabrielalmeida
Автор

Love this, thank you. It has gone some way to helping me with recursion in general.

BluetonicUK
Автор

Thank you for the video. This helped me to solve one of the question in my assignment. Keep going!

waynefong
Автор

Thanks for the upload. Even debugging that code from the stack overflow post I couldn't really fully understand what was happening x_x this helped a lot.

jazzdayz
Автор

going through the code step by step really helped me understand. Thank you so much!

boshiij
Автор

thank you for stepping through an example! so helpful!

adebs
Автор

I like your video, algorithm and explanation. Is this a legal binary tree? how would you add node values to git this tree?

timr
Автор

also, when I see algorithms for checking if the tree is balanced they change the meaning of height and use -1 to indicate a non-balanced height. they use a 0 height for null node. I realize this is a trick to reduce the O() complexity of the algo, but it kindof goes against the grain to have a method getHeight(Node root) that doesn't actually return the height. Do you have a algo that determines if a tree is balanced?

timr
Автор

Hi man, why you stopped uploading videos, they are very instructive, thanks!

dsclips
Автор

Sir, what's the time complexity for finding height of balanced binary search tree

jitendertehlan
Автор

This is incorrect. Had the left side been the longest path from root to a leaf node then the height would have been 2 not 1. That -1 nonsense is just that. The height of a leaf node is always 0 not -1.

Brandon-ooqi