Тёмный
No video :(

Find the Maximum Depth or Height of a Tree | GeeksforGeeks 

GeeksforGeeks
Подписаться 717 тыс.
Просмотров 126 тыс.
50% 1

Find Complete Code at GeeksforGeeks Article: www.geeksforgeeks.org/write-a-...
Practice Problem Online Judge: practice.geeksforgeeks.org/pro...
Read More: www.geeksforgeeks.org/find-th...
This video is contributed by Anant Patni
Please Like, Comment and Share the Video among your friends.
Also, Subscribe if you haven't already! :)

Опубликовано:

 

6 авг 2017

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 58   
@parthaprateempatra4278
@parthaprateempatra4278 4 года назад
If you want to count the number of edges return -1 instead of 0 in the exit case
@harshitrathi3077
@harshitrathi3077 3 года назад
YES
@dinner4chiahao
@dinner4chiahao 2 года назад
How come?
@dishant930
@dishant930 4 года назад
thanks helps alot!
@linus430
@linus430 2 года назад
Thank you!
@BharatVaad
@BharatVaad 5 лет назад
Thanks brother
@Saddam_Al-zekri
@Saddam_Al-zekri 2 года назад
Thank you so much
@pawarranger
@pawarranger 5 лет назад
The height of a binary tree is the number of edges between the tree's root and its furthest leaf. In this case, it's 2, not 3.
@rishabhsharma667
@rishabhsharma667 4 года назад
He's following 1st convention
@ehabreda365
@ehabreda365 4 года назад
@@rishabhsharma667 is there is a different
@inderpreetkaur5781
@inderpreetkaur5781 4 года назад
@@rishabhsharma667 The root node has depth zero, leaf nodes have height zero, and a tree with only a single node (hence both a root and leaf) has depth and height zero. Conventionally, an empty tree (tree with no nodes, if such are allowed) has height −1.
@reiniergarcia
@reiniergarcia 4 года назад
The first convention has a lot more sense. Is more logical. Even when we graphically represent the tree, it has A LOT more sense the first convention.
@pawarranger
@pawarranger 4 года назад
@@reiniergarcia height implies some kind of length. when we measure length, for eg 3 units on a scale, we never count the point 0 on the scale. idk how the 1st convention makes more "sense", however the 0 convention is more consistent with the definition of length as we know it.
@gokulnath589
@gokulnath589 Год назад
Cal i do store values in arraylist and then sort it easily get min and max value is it right approach
@harshitrathi3077
@harshitrathi3077 3 года назад
BY YOUR CASE if there is only root than height is 1 .... is it right ?
@LastManRising
@LastManRising 6 лет назад
thanks 4 this wonderful tutorial.very help ful
@GeeksforGeeksVideos
@GeeksforGeeksVideos 6 лет назад
Thanks abhirup banerjee!
@unkowntaco965
@unkowntaco965 Год назад
god i love geeksforgeeks
@AbdulRahman-er3dz
@AbdulRahman-er3dz 3 года назад
Height of tree is 2 not 3 ,you sre counting the nodes instead count the edges ,height is maximum number of edges from root to leaf
@unstoppable9668
@unstoppable9668 3 года назад
yeah bro i am also having the same doubt so what is correct?
@ahmetcevahircinar5699
@ahmetcevahircinar5699 6 лет назад
Thank you.
@GeeksforGeeksVideos
@GeeksforGeeksVideos 6 лет назад
Thanks Ahmet Cevahir ÇINAR :)
@kostasemex4607
@kostasemex4607 2 года назад
I need help. What I must write in main in order to print the longest path?
@purushotamkumar9596
@purushotamkumar9596 2 года назад
just print final return of called function
@bmrs2011
@bmrs2011 4 года назад
Coding is correct but explanation is wrong. maxDepth(1) -> maxDepth(2) -> maxDepth(4) maxDepth(4) is 1 not 0 , it has lDepth = 0, rDepth = 0, as a result it gets 1 going by the algorithm.
@53anubhav
@53anubhav 6 лет назад
Can you please explain how do we compute time complexity of such solutions ? Thanks.
@DjRoshSydney
@DjRoshSydney 6 лет назад
Since we iterate over all the nodes for calculating the height, the time complexity is O(n)
@shubhamshinde5643
@shubhamshinde5643 4 года назад
@@DjRoshSydney it depends becoz O(n) for worst case and O(log2n) for perfect binary tree
@oshershuman7714
@oshershuman7714 2 года назад
@@shubhamshinde5643 no its always O(n). Regardless what the tree is like, you always have to traverse every node in order to know the depth because you wouldn't know its a perfect binary tree.
@Brianvanvlymen
@Brianvanvlymen 3 года назад
empty BST return should be -1 since the BST is not existed, if existed return should be 0 of height
@soumyajitdutta2324
@soumyajitdutta2324 3 года назад
when ldepth and rdepth are equal, then how ldepth becomes 1?
@saranshupadhyay3123
@saranshupadhyay3123 3 года назад
same doubt
@shrey5959
@shrey5959 2 года назад
@@saranshupadhyay3123 because the function executes else part which return 0+1 to the ldepth.
@nishantgarg7497
@nishantgarg7497 6 лет назад
At ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-TQI_m32_AeU.html the rdepth and ldepth are equal i.e. 0 and according to the algorithm if ldepth is > then rdepth then we add ldepth to ldepth+1, but in video we added ldepth+1 even though both are equal.. Am I getting it wrong or is it something else???
@harshitrathi3077
@harshitrathi3077 3 года назад
if " if " part fails than else part is executed automatically so it should be rightdepth+1
@sanchitjain9745
@sanchitjain9745 2 года назад
great
@Farreach
@Farreach 5 лет назад
I don't understand why i segment fault with this code
@piyushkandwal
@piyushkandwal 4 года назад
At time 3:31 isn't the rDepth should be 1 instead of lDepth??
@shubhamshinde5643
@shubhamshinde5643 4 года назад
Bro listen I also confused at beginning but look like this way left side is returning while right side is still executing (2 was left before so it get the returning value but at that point right start executing because function is over of indepth) when function finishes it start executing it's next part rather than completely exiting the whole functions it complicated but it behave in that mannner
@harshitrathi3077
@harshitrathi3077 3 года назад
@@shubhamshinde5643 Bro when ever "if" part fails or return false than else part is executed automatically , so when 0>0 in if part fails than directly else part is executed and rightdepth becomes 1 .
@oggy107
@oggy107 3 года назад
@@harshitrathi3077 did you find answer to this one? i am confused too
@randomvideos3628
@randomvideos3628 6 лет назад
it could be accomplished in an one liner... return node == NULL ? 0 : max(maxDepth(node->left), maxDepth(node->right)) + 1;
@lesa251
@lesa251 4 года назад
Function parameters are not evaluated in a defined order in C.
@vinaykumarbandarapalli216
@vinaykumarbandarapalli216 3 года назад
Height of a Tree is 2 only
@CorporateMajdoorTrader
@CorporateMajdoorTrader 2 года назад
Explanation was so vague😕
@rajan5828
@rajan5828 3 года назад
Height is 2 I think
@inderpreetkaur5781
@inderpreetkaur5781 4 года назад
The height is 2 not 3.
@yahyashaikhworld
@yahyashaikhworld 4 года назад
2+1 = 3
@surajkumarmohanty4976
@surajkumarmohanty4976 3 года назад
kaun sa nasha krti ho behn
@oggy107
@oggy107 3 года назад
it's 3
@karinhsu9302
@karinhsu9302 4 года назад
Can someone confirm that the height is 2 and not 3?
@shubhamshinde5643
@shubhamshinde5643 4 года назад
Yes height is 2
@shubhamshinde5643
@shubhamshinde5643 4 года назад
Empty tree height is -1 while single node it is zero . In fact height == number of arrows in that path
@R-khan3060
@R-khan3060 2 года назад
Hindi me chahiye ye video
@chase7914
@chase7914 4 года назад
wtf do you do if it is generic tree (can have more than 2 children) I cant FIND ANYTHING trying to find videos about it only binary trees...Fuck my prof
@1656456
@1656456 2 года назад
more explanation next time...
@gauravbhisikar6381
@gauravbhisikar6381 4 года назад
Watch at 1.5x speed
Далее
AVL Tree - Deletion | GeeksforGeeks
2:37
Просмотров 86 тыс.
And what is your height? 😁 @karina-kola
00:10
Просмотров 1,3 млн
Learn Binary search trees in 20 minutes 🔍
20:25
Просмотров 151 тыс.
Premature Optimization
12:39
Просмотров 777 тыс.
Dijkstra's Algorithm - Computerphile
10:43
Просмотров 1,3 млн
Diameter of Binary Tree - Leetcode 543 - Trees (Python)
11:16