Height of a Binary Tree / Maximum depth of a binary tree Algorithm [REVISITED]

Sdílet
Vložit
  • čas přidán 7. 09. 2024

Komentáře • 225

  • @codewithree930
    @codewithree930 Před 3 lety +33

    Wow! Finaly I got someone who have deeply explained it..I got the feel...
    Thanks a lot🙂

    • @chaminduperera3998
      @chaminduperera3998 Před 3 lety

      mmmmm....

    • @Man_of_Culture.
      @Man_of_Culture. Před 3 lety +1

      Yep, he gives us the feel of ds

    • @anandoganiya9070
      @anandoganiya9070 Před 3 lety +1

      not like apna college

    • @Man_of_Culture.
      @Man_of_Culture. Před 3 lety

      @@anandoganiya9070 both of them are good , I follow many CZcams channel on DS and Algo because in the end it's about learning and learning .

    • @anandoganiya9070
      @anandoganiya9070 Před 3 lety

      @@Man_of_Culture. ik but they are running which leads to misunderstanding and lots of confusion but this guy explains it just right best for beginners

  • @vicky888
    @vicky888 Před 3 lety +14

    I've never seen someone has such great patience,really good explanation!

  • @KeithWarter
    @KeithWarter Před 4 lety +24

    Thank you for these videos! They are much appreciated!!!! This stuff needs to be slowed way down to grasp it at first just like a musical instrument. You taking the time to white board it like this is really really helpful.

  • @amoghgowda6117
    @amoghgowda6117 Před 3 lety +1

    I have seen many videos, Everyone says call the method recursively to find left height and right eight and find the max btw them. But no one told how the code is running line by line. But your the only person who showed how recursion works with 123 way of teaching, Loved the video.

  • @anupamdey4893
    @anupamdey4893 Před 3 lety +2

    Thank you for the in-depth explanation. No other video on YT explains the recursive calls which is the most imp part of trees.

  • @farahluvs1d
    @farahluvs1d Před 4 lety +1

    thank you, this is the best most detailed explanation i found,rather than just writing the code and explaining in 5 monutes

  • @vaishali3339
    @vaishali3339 Před 2 lety +1

    Was about to have a breakdown because I was not getting this problem but your video made it very easy to understand. Thank you for the step by step explanation. धन्यवाद!

  • @samyakjain8384
    @samyakjain8384 Před 5 lety +1

    Explaining recursion is very tough and you explained it in very easy manner

  • @xiaolongli1898
    @xiaolongli1898 Před 4 lety +6

    There is an error:
    for the last part:
    if(left > right) {
    int h = 1 + left;
    }
    else {
    int h = 1 + right;
    }
    return h;

  • @love_piano_3000
    @love_piano_3000 Před 7 měsíci +1

    I have literally searched every video related to this topic and this one cleared all my doubts , Thanking you so much Sir... 😃😃😃

  • @pauldaviddavies
    @pauldaviddavies Před 3 lety

    Wow, four years ago yet explaining the concept that deep as if it is being done today. Vivekanand that is great and deep. Thank you.
    Where are my likes for him guys?

  • @mouseclicker4955
    @mouseclicker4955 Před 3 lety

    Looked for explanations everywhere and you were the only one that explained it clearly enough for me to understand. Thank you!

  • @danielchia1389
    @danielchia1389 Před 4 lety +2

    This is helped me understand it after watching other videos that didnt explain it as detailed as you. Thank you !!

  • @shivamnerwal72
    @shivamnerwal72 Před 3 lety +2

    You are doing a great work sir ,
    Your tutorials are really very helpful
    🙏🙏

  • @rishabhjaiswal3276
    @rishabhjaiswal3276 Před 2 lety

    Your are gem sir , I saw so many video but after watching your video I got the how its working , keep posting videos :)

  • @VikiSangani
    @VikiSangani Před 5 lety +2

    Very easy and to the point explanation. Presentation is also very nice. Thank you for sharing knowledge. Looking forward the same in future.

  • @kartiksuman9814
    @kartiksuman9814 Před 3 lety

    very well explained bhaiya....hmko ni lg atha ki mai isse samjh paunga, but you really made it possible...thanks a lot..

  • @ryujiganaha9645
    @ryujiganaha9645 Před 4 lety +5

    Thank you so much for the great explanation! Big appreciation from Japan

  • @viveknandan4950
    @viveknandan4950 Před rokem

    Never thought someone can explain it so deeply... Thank you so much

  • @shreyassule2219
    @shreyassule2219 Před 3 lety +2

    Hi Sir, Thank you so much for these videos. Your style of explaining algorithms with examples and then walking us through each step of the code really helps to grasp the concept very effectively. If I may be allowed for just one small suggestion can you also please explain the time complexity for these? Regardless, great fan of your teaching. Keep up the good work!!

  • @vickyhu6970
    @vickyhu6970 Před 4 lety +2

    You are excellent at explaining things clearly! Thank you for this video.

  • @rishipalbhatia
    @rishipalbhatia Před 6 lety +2

    If you compile this code, you will get an error on the line return h;
    That's because the two declarations of variable h are inside the scope of if { } and else { }. You're trying to use h outside it's scope.

  • @LarryRuane
    @LarryRuane Před 7 lety +18

    Thanks, great explanation. Just one small suggestion with the code -- the variable h is declared locally within the "if" and "else" blocks, so is no longer in scope for the "return" statement. So you would need to declare h before the "if" statement.

    • @usman3D
      @usman3D Před 6 lety +1

      Exactly my thoughts

  • @arjunsankarlal5296
    @arjunsankarlal5296 Před 3 lety

    Very slowly explained in the start. But you made it clear! Thanks :)

  • @modernsanskari4398
    @modernsanskari4398 Před 7 lety +16

    In step 3 you can directly write.
    h = max(left , right) + 1;
    return h;

    • @yoonlee5278
      @yoonlee5278 Před 5 lety +3

      I think he meant to explain purely used conditional statement for the people to understand more easily.

    • @reyou7
      @reyou7 Před 5 lety +2

      You can take entire code, and put in a text box, and turn into a single line.
      shorter != better.

  • @jammy2003
    @jammy2003 Před 2 lety

    THANKS SOO MUCH!!! I had a great deal of difficulty to understand the recursion portion of this solution! You explained it very easily! Thanks again man!

  • @anjalidarokar2408
    @anjalidarokar2408 Před 4 lety +1

    Sir, the height of a binary tree is the number of edges in the longest path from the root to the leaf, not the number of nodes from the root to the leaf, therefore, here, height = 4.

    • @Sun-yv9fr
      @Sun-yv9fr Před 4 lety

      you are talking about the depth. Depth is 4 and height is 5.

    • @adityasagar9293
      @adityasagar9293 Před 3 lety

      @@Sun-yv9fr google it...

  • @KamalKumar-bk6rn
    @KamalKumar-bk6rn Před rokem

    Thank you Vivekanand for the clear explanation.

  • @ritik8879
    @ritik8879 Před 3 lety

    Excellent explanation for recursion . Really you have a very good understanding of dsa

  • @piyushsharma1638
    @piyushsharma1638 Před 5 lety

    I really liked the way you show small details like, why we add 1 in finding the height of a binary tree, most of of the videos simply add 1 and not explain any reason behind it, i know it is easy part but at least you explained.

  • @vuramvincent721
    @vuramvincent721 Před 2 lety

    Thank you so much !!! you have cleared all the confusion for the algorithm for searching the height of BST

  • @jamesqiu6715
    @jamesqiu6715 Před 7 lety +1

    appreciate you make a revisited video to cover previous doubt on height definition. good job.

  • @RN-jo8zt
    @RN-jo8zt Před 6 měsíci

    Def: The height of a node in a binary tree is the largest number of edges in a path from a leaf node to a target node
    but why we still adding 1?

  • @mfgucluer
    @mfgucluer Před rokem

    THANK YOU VERY MUCH SIR. Your ability to express is very good.

  • @ashishchaudhari8223
    @ashishchaudhari8223 Před 3 lety

    Thnks sir by explaing thic concept of maxdepth and also recursion function

  • @sunitamishra5475
    @sunitamishra5475 Před 2 lety

    Finally someone explained it....thank you so much

  • @raunak02
    @raunak02 Před 3 lety

    Thanks a lot sir, you have removed all my confusion created by my prof.

  • @souravghosh7093
    @souravghosh7093 Před 4 lety

    This is the best explanation i found. Thanks. Need to watch at 2x

  • @iasaspirant1441
    @iasaspirant1441 Před 2 lety +1

    In code everything is correct but return value is -1 so we get height of binary tree is equal to 4 not 5

  • @abdelrhmanahmed1378
    @abdelrhmanahmed1378 Před 3 lety

    int find_height(BstNode* root)
    {
    if (root == NULL)
    return -1;
    else return max(find_height(root->left), find_height(root->right)) + 1;
    }
    you need to return -1 ,because last recursion on the leaf node must return 0 ,because the height of the
    leaf node is 0 so max(-1,-1) +1 --> will evaluate to 0
    and for those who are intelligence enough will realize that when we return to the root node after we finish the left recursion the height will be shorter by one ,that one will get added after we finish the right sub-tree
    if the right sub-tree is shorter that the left so that one that get added will get the us right height ,and vice versa if the right height
    is longer

    • @abdelrhmanahmed1378
      @abdelrhmanahmed1378 Před 3 lety

      and by the way the definition of tree height is the max edges from the root to the longest path
      to the leaf ,so its not 4 or 5 there is only one right answer which is 4 for this example

  • @2010aishwary
    @2010aishwary Před 4 lety +1

    in this way the height of the binary tree with only root node will be 1 but the actual height of binary tree with only root node is 0 ??

  • @onyinyechichukwuma689
    @onyinyechichukwuma689 Před 3 lety

    The best explanation I've seen online. Thanks very much!

  • @prinsedits
    @prinsedits Před 2 lety

    thankyou so much sir for such a detailed explanation

  • @chauhanrachit
    @chauhanrachit Před 6 lety +3

    Can you please create playlists under your channel ? Like trees, dynamic programming, graph

  • @vivekmit
    @vivekmit Před 4 lety

    In a nut-shell ,we are traversing the a binary tree to count the level at each node. Can you brief we, which kind of binary traversal we implemented in this video ? Can we implement same logic with different kind of traversal like inoder/preorder ?

  • @mdanishossain026
    @mdanishossain026 Před 2 lety

    Tremendous explainatio sir, ❤from BD

  • @m.motivation6299
    @m.motivation6299 Před 2 lety

    The best explanation ever

  • @vishnu_bhatt
    @vishnu_bhatt Před 6 lety

    C++ code:
    int height(Node* root) {
    if(root==NULL || root->left==NULL && root->right==NULL) {
    return 0;
    }
    int left = height(root->left);
    int right = height(root->right);
    if(left>right) {
    return left+1;
    } else {
    return right+1;
    }
    }
    The height of a node is the number of edges on the longest path between that node and a leaf.
    The level of a node is defined as: 1 + the number of edges between the node and the root.

  • @bryanlee5522
    @bryanlee5522 Před rokem

    Thank you, I understand after seeing step by step

  • @chetanjain5097
    @chetanjain5097 Před 2 lety

    You explain better than anyone else. Most videos explain logic but don't have code. You do show code. But I just haven't seen any single video that explains with test cases (with input data) and code. After viewing your video I am still now sure what value of node is passed to the function. Is it pointers to left and right edge or is it something else?

  • @gokarnapandey4930
    @gokarnapandey4930 Před 3 lety

    Wow sir 🥰🥰....I could not hold myself to appreciate your explanation...
    Thank you sir...

  • @JasPer22996
    @JasPer22996 Před 3 lety

    Well said. Good explanation. Greetings from new york......
    Cubao

  • @yasirshaikhkhan6189
    @yasirshaikhkhan6189 Před 5 lety

    Thanks sir.... your clear explanations and presentation help better to understood..

  • @juhisingh4509
    @juhisingh4509 Před 4 lety

    Very good explaination of recursion ...Thanku sir

  • @preetichauhan3272
    @preetichauhan3272 Před 6 lety

    Thanks Vivekanand for your videos, your algorithms helps me a lot in learning Java.

  • @vijaysagoo2464
    @vijaysagoo2464 Před 3 lety

    Thank you for the in-depth explanation

  • @prashanthchinthilla3968

    By this i learned recursion too thanks:)

  • @nitinmonga6975
    @nitinmonga6975 Před 4 lety

    Very well explained sir.. Thank you so much

  • @JULIUSNGIGIux
    @JULIUSNGIGIux Před 3 lety

    Please update this video with the correct information. The height of a binary tree is the number of edges in the longest path from the root to the leaf, not the number of nodes from the root to the leaf.

  • @amangarg6531
    @amangarg6531 Před 7 lety +1

    sir please make the video on clone a binary tree with random pointers and also bactracking concept. your videos are very helpful for us🙂

  • @ShreyasGaonkar
    @ShreyasGaonkar Před 3 lety

    Great explanation on this and the video about the diameter of a tree. Was able to solve the leetcode question on the first go. Sub'ed :)

  • @adarshlakshmanan
    @adarshlakshmanan Před 6 lety

    The height of the tree is calculated wrong. Counting the edges, the height is 4, not 5.
    This is how you correct it:
    In the program,
    if (p==null){
    return -1; //Instead of 0 as shown in the video.
    }
    This also make sense conceptually since the depth of a Null node is -1 & not 0.

    • @sumanreddyg8245
      @sumanreddyg8245 Před 5 lety

      So for a NULL tree what is the height you want to return -1 ? I hope 0 is right answer, if zero is right then height is 5 not 4, for NULL tree both depth and height must return 0

  • @aditi_jha
    @aditi_jha Před 3 lety

    Best explanation ever!!!!

  • @shreyasangane3822
    @shreyasangane3822 Před 4 lety

    Your are Awesome !!!!!!!!!!!!!!!!!!!!! You made it very simple and clear.

  • @prathameshautade2679
    @prathameshautade2679 Před 3 lety

    Really good explanation, keep up the good work

  • @gauravbhagat8831
    @gauravbhagat8831 Před 3 lety

    Got in one go thank you Vivekanand !

  • @shilpitamedhi3204
    @shilpitamedhi3204 Před 3 lety

    great explanation thank u sir

  • @rajivsarkar277
    @rajivsarkar277 Před 3 lety

    M not able to understand recursion . can somone help me to understand that

  • @anandoganiya9070
    @anandoganiya9070 Před 3 lety

    bro what to say far better than apna college

  • @maazalam66
    @maazalam66 Před 7 lety +1

    Really nice explanation. ...thank you ...keep making videos

    • @coderajput1816
      @coderajput1816 Před 7 lety

      no it is not working properly....if any mistake in my code then please comment
      i uploaded the code in my comment

  • @tumble_dont_fall
    @tumble_dont_fall Před 4 lety

    best explanation on the internet

  • @waynegreen7970
    @waynegreen7970 Před 3 lety

    Good content! Detailed and precise explanation!

  • @FamousEgyptboy
    @FamousEgyptboy Před 6 lety +1

    Thank you sir for great explanation !

  • @SICSSonamSharma
    @SICSSonamSharma Před 3 lety

    Amazing explanation!

  • @revanth9272
    @revanth9272 Před 3 lety

    thank you sir. worth to watch your videos..!!

  • @Gudsan
    @Gudsan Před 5 lety

    very nice explanation sir

  • @itis_beard_man2471
    @itis_beard_man2471 Před 4 lety +1

    Good, I understand this very easily thank you!

  • @ArunKumar-gp2vq
    @ArunKumar-gp2vq Před 3 lety

    Thank you so much brother you are doing great job

  • @jitendragupta4661
    @jitendragupta4661 Před 4 lety

    The best explanation!Keep posting!

  • @100_bindusai8
    @100_bindusai8 Před 4 lety

    Easy to understand 😀😊

  • @ranitsinha6424
    @ranitsinha6424 Před 6 lety

    The height of this tree will be 4 not 5 .. please correct it. For more info please visit --->www.hackerrank.com/challenges/tree-height-of-a-binary-tree/problem

  • @jeezradz
    @jeezradz Před 4 lety

    what is the time complexity for this???

  • @sasikiranjutury5296
    @sasikiranjutury5296 Před 4 lety

    The Code is wrong bro. Here you are updating the value of h but not left or right.For every recursion the left and right value remains same.Please update the code as
    if(left>right)
    return(left+1)
    else
    return(right+1)

    • @mr.poopybutthole3719
      @mr.poopybutthole3719 Před 4 lety

      Nothing is wrong with the code bro. He has just introduced a new variable H for returning the value,you can return (left+1) directly or via storing it in a variable.😊

  • @lokeshchennamchetty1019

    height doesn't include root node, so height of node is 4 not 5 ,need correction!!

  • @iasaspirant1441
    @iasaspirant1441 Před 2 lety +1

    Sir everything is correct but height of binary tree is equal to 4 sir not 5

  • @tayrx9988
    @tayrx9988 Před 4 lety +2

    OMG finally understood this bullshit, thanks so much

  • @jaysahu357
    @jaysahu357 Před 7 lety

    very nice explaination sir

  • @DeepakGupta-pt5rv
    @DeepakGupta-pt5rv Před 7 lety +2

    Sir,You are the best!!

  • @MrRimmy
    @MrRimmy Před 6 lety

    VERY GOOD EXPLAINATION

  • @harshitraj4199
    @harshitraj4199 Před 3 lety

    finally mil hi gya.... thanks a lot

  • @deepakborah8335
    @deepakborah8335 Před 6 lety

    Very good explanation.

  • @raj-nq8ke
    @raj-nq8ke Před 3 lety

    perfect as it should be.

  • @ankitathakur5989
    @ankitathakur5989 Před 4 lety

    Thanku so much sir ji

  • @saldanhakaren1
    @saldanhakaren1 Před 6 lety

    really good videos, looking forward to more content

  • @mohamedelkayal8871
    @mohamedelkayal8871 Před 7 lety

    Your videos have been very helpful

  • @coderajput1816
    @coderajput1816 Před 7 lety +1

    but this code is not running properly....its wrong
    #include
    #include
    struct tree
    {
    int data;
    struct tree *left,*right;
    };
    int height(struct tree *ptr)
    {
    if(ptr==NULL)
    return 0;
    int lh=height(ptr->left);
    int rh=height(ptr->right);
    if(lh>rh)
    return(lh+1);
    else
    return(rh+1);
    }
    struct tree *newNode(int data)
    {
    struct tree *t;
    t=(struct tree *)malloc(sizeof(struct tree));
    t->data=data;
    t->left=t->right=NULL;
    return t;
    }
    int main()
    {
    struct tree *root;
    root=newNode(4);
    root->left=newNode(5);
    root->right=newNode(10);
    root->left->left=newNode(24);
    root->left->left->left==newNode(2);
    root->left->right=newNode(34);
    root->right->left=newNode(12);
    printf("%d",height(root));
    return 0;
    }
    my codes output is 4 but it is producing 3...why?
    i follow ur video
    ur explanation is good but why it is not giving correct answer

    • @rahulraghav7792
      @rahulraghav7792 Před 6 lety +1

      "root->left->left->left==newNode(2);" this is wrong..!! only = not ==

  • @MohitSharma-hd7gj
    @MohitSharma-hd7gj Před 6 lety +1

    sir return h; if and else blocks mai aayega

  • @sagarkolhe5927
    @sagarkolhe5927 Před 4 lety

    great job vivekanand....

  • @natesh1
    @natesh1 Před 6 lety +15

    Many times I forget that I am watching in 2 x speed. Lol!

  • @azizas9366
    @azizas9366 Před 6 lety

    I like your explanation algorithm