Interview Question: Balanced Binary Tree

Sdílet
Vložit
  • čas přidán 28. 08. 2024
  • Coding interview question from www.byte-by-byt...
    In this video, I show how to determine whether a binary tree is balanced.
    Do you have a big interview coming up with Google or Facebook? Do you want to ace your coding interviews once and for all? If so, Byte by Byte has everything that you need to go to get your dream job. We've helped thousands of students improve their interviewing and we can help you too.
    Stuck on Dynamic Programming? Check out our free ebook: www.dynamicprogrammingbook.com
    50 Practice Questions: www.byte-by-by...
    You can also find me on
    Twitter: / bytebybyteblog
    Facebook: / bytebybyteblog
    Email: sam@byte-by-byte.com

Komentáře • 41

  • @grapegripe
    @grapegripe Před 2 lety

    You can simplify the body of isBalanced to “return (balancedHeight(n) > -1);”

  • @kingofgods898
    @kingofgods898 Před 3 lety

    DAMN this is a clean solution. Good shit ByteByByte!

  • @jiyi123451
    @jiyi123451 Před 7 lety +7

    ...
    int h1 = balancedHeight(n.left);
    if(h1 == -1) return -1;
    int h2 = balancedHeight(n.right);
    if(h2 == -1) return -1;
    ...
    Could this save some time if h1 is not balanced?

    • @ByteByByte
      @ByteByByte  Před 7 lety +5

      Yeah you could totally do that. Doesn't change the big O complexity but does improve the average case

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

    You do not have to invoke the balancedHeight method for the right subtree if h1 == -1.

  • @oleksandrknyga868
    @oleksandrknyga868 Před 5 lety

    You can also use tree traversal to calculate min and max height of the leaf
    function isBalanced(node) {
    let max = Number.MIN_SAFE_INTEGER;
    let min = Number.MAX_SAFE_INTEGER;
    const postFix = function(node, level) {
    if(!node) {
    max = Math.max(max, level);
    min = Math.min(min, level);
    return level;
    }
    const rLevel = postFix(node.right, level+1);
    const lLevel = postFix(node.left, level+1);
    const c = Math.max(rLevel, lLevel) + 1;
    return c;
    };
    postFix(node, 0);
    if(max-min < 2) {
    return true;
    }
    return false;
    }

  • @ivanchl
    @ivanchl Před 3 měsíci

    Why does an unbalanced tree produce -1? Can you please show?

  • @praveenbanthia5443
    @praveenbanthia5443 Před 6 lety +6

    Great video just needed one clarification . Conceptual is slightly different from implementation right ?
    The height of root node should be 2( as per definition of height) but we end up returning 3. Why are we doing so ?

    • @yolopassion
      @yolopassion Před 4 lety

      I have the same question.Did you get your answers?

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

    A small correction here , u mentioned in one of the statements that u cant overload methods becoz of different return types , but actually u can overload methods wid different return type as long as the parameters u pass r different in order ,type or number.

  • @neb542
    @neb542 Před 4 lety

    But height never gets values.... You do not add 1 during each recursive call???? What am I missing, Thanks good video!

  • @baraatubishat403
    @baraatubishat403 Před 4 lety

    please can you tell me the worst case complexity (Big-Oh) for this code in the video?

  • @tushargoel5522
    @tushargoel5522 Před 4 lety

    Do we really require if (h1 == -1 || h2 ==-1) statement? Without this even it is returning -1 in case of tree hight difference is > 1.

  • @arielle-cheriepaterson3317

    Why aren't the elements of the tree in order if the whole point of a binary search tree is to have every node be balanced from left to right?

    • @ByteByByte
      @ByteByByte  Před 7 lety +26

      A binary tree is not necessarily a binary search tree

  • @richardge634
    @richardge634 Před 8 lety +5

    why whould the height be -1?

    • @ByteByByte
      @ByteByByte  Před 8 lety +9

      We're just using -1 as an indicator that the tree is not balanced. That way if we have a positive value, we know that the tree is balanced AND the height is that value

  • @memorablename5187
    @memorablename5187 Před 7 lety +2

    I did this 2 years ago in school, cant believe people ask about trees in interviews. Do professionals ever deal with trees really? at least not implementing their own surely

    • @ByteByByte
      @ByteByByte  Před 7 lety +11

      You're highlighting a key point, which is that interviewing is a completely separate skill from actually working as a software engineer. Unfortunately, you need to know this for your interview despite it not being relevant to your actual work

    • @memorablename5187
      @memorablename5187 Před 7 lety +6

      I had my first software engineering interview last week, and they asked me to about binary tree's. I am so glad I watched this.

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

      Did they ask you to actually implement one or did they just ask you to explain the concept?

    • @memorablename5187
      @memorablename5187 Před 7 lety +4

      they gave me code, asked me what it did. It instantly saw it was a BST, then they asked me about functions i would add to it, (insert, delete,balance,find,ect...) and i had to pseudo code the functions

  • @vaibhavlodha5398
    @vaibhavlodha5398 Před 5 lety

    Great video...very nice and simple explanation!! Thank you.

  • @marthacornejo3710
    @marthacornejo3710 Před 7 lety

    hey! great videos! I don't understand when/how h1 or h2 would could be -1? because we're not initializing them to -1? or is null -1 in integer?

    • @ByteByByte
      @ByteByByte  Před 7 lety

      basically the latter. int is a primitive type so it is initialized to 0. By setting them to -1 that is the equivalent of saying the value is null

  • @chhaviagarwal7514
    @chhaviagarwal7514 Před 4 lety

    hey can you please tell why we are checking this condition (h1 == -1 || h2 == -1) return -1;

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

      I believe this is because we are checking if the left and right subtrees are balanced as well as the original tree. He is using -1 as an indicator that the subtree is not balanced (since -1 is not a valid height). If the left or right subtree is unbalanced then the entire tree is unbalanced, so we must make sure they are balanced before computing the height for our current root. I hope this is helpful :)

    • @chhaviagarwal7514
      @chhaviagarwal7514 Před 4 lety

      @@sniperbear2000 Thanks I got it

    • @iqtidarnewaz1691
      @iqtidarnewaz1691 Před 2 lety

      @@sniperbear2000 hi, can you pls talk about an example case where h1 or h2 == -1? I understand your explanation but unable to find a case where it hits -1 base

  • @ronaldabellano5643
    @ronaldabellano5643 Před 5 lety

    This was not easy, thinking to initialise the program to -1.

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

    where did -1 come from in the helper method? how can h1 and h2 equal -1?

    • @amellperalta
      @amellperalta Před 7 lety

      -1 has been used as an error code to indicate that the tree is not balanced (i.e., the difference in height between left and right subtree of a particular node is greater than 1). You could also use another value as an error code (e.g., Integer.MIN_VALUE). Actually, the height of an empty tree is defined to be -1, so it would be better to return -1 if n == null and INTEGER.MIN_VALUE as an error code.

  • @harshaldeolekar5634
    @harshaldeolekar5634 Před 7 lety

    I didn't understood Math.abs(h1 - h2) > 1. What exactly do you mean by "not more than one difference" ?

    • @alexpeterson6944
      @alexpeterson6944 Před 7 lety

      A binary tree in this case is not balanced if any subtree has a height of 2 or more than another subtree.

  • @brucewang2072
    @brucewang2072 Před 6 lety

    2:19 You are wrong about the definition of binary search tree height. It is as follows: The heights of two subtrees of any node are no more different than 1.

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

      That's what I said. If the left subtree and the right subtree don't differ by more than 1

    • @brucewang2072
      @brucewang2072 Před 6 lety

      First of all, your definition of subtree is not correct based on the way you explains subtree height in your video. (You can check this link out: www.hackerrank.com/challenges/tree-height-of-a-binary-tree/problem.) Second, the left and right subtrees' heights differ by at most one, the left subtree is balanced, and the right subtree is balanced. The tree instance you showed is thus not a balanced tree.

  • @Nyquiiist
    @Nyquiiist Před 3 lety

    Your definition of height here is incorrect.

  • @rajeshdansena
    @rajeshdansena Před 6 lety

    1:24 is not Balacned. 3 LST length is 3 and RST is 0