L15. Check for Balanced Binary Tree | C++ | Java

แชร์
ฝัง
  • เผยแพร่เมื่อ 9 พ.ย. 2024

ความคิดเห็น • 317

  • @takeUforward
    @takeUforward  3 ปีที่แล้ว +127

    Please likeeee, shareeee and subscribeeeeeeee :) Also follow me at Insta: Striver_79

    • @jaydeeppurohit1
      @jaydeeppurohit1 3 ปีที่แล้ว

      thank's man for the free tutorials very helpful

    • @democratcobra
      @democratcobra 3 ปีที่แล้ว

      Incredible mind.

    • @SatyamEdits
      @SatyamEdits 2 ปีที่แล้ว

      Bhaiyaaa...."in Brief" mtlb "in short" hota hai......😅😅

    • @AmanAshutoshBCS
      @AmanAshutoshBCS ปีที่แล้ว

      Sir, Timestamp 8 minute and 10 second. Sir, I am unable to understand in what case we will be getting -1 for lh and rh.???

    • @pulkitjain5159
      @pulkitjain5159 ปีที่แล้ว

      i think on 9:21 , it should be if( lh ==-1 || rh == -1) return -1 , beacuse if any of the subtree is unbalanced , my whole tree is unbalanced. if does'nt require to have my both subtree unbalanced.

  • @リンゴ酢-b8g
    @リンゴ酢-b8g 2 ปีที่แล้ว +41

    A balanced binary tree, also referred to as a height-balanced binary tree, is defined as a binary tree in which the height of the left and right subtree of any node differ by not more than 1.

  • @abhishekarora437
    @abhishekarora437 3 ปีที่แล้ว +121

    Never thought that this question can be solved like this also....very easy code
    A big thank you to striver

  • @kennethantonyjohn2708
    @kennethantonyjohn2708 2 ปีที่แล้ว +18

    Thanks a lot for the explanation, it was very helpful.
    One further optimization that could be done is to check if lh and rh values right after each recursive call
    lh = check(node -> left)
    if lh == -1:
    return -1
    rh = check(node -> right)
    if rh == -1:
    return -1
    In this case we are not computing the right sub-tree if at all, at any point the left tree breaks the condition

    • @chiragvohra6673
      @chiragvohra6673 2 ปีที่แล้ว +1

      how lh value will be -1 can u explain ?

    • @VV-ps8rt
      @VV-ps8rt ปีที่แล้ว

      @@chiragvohra6673 if at any node in left subtree is not balanced, ultimately will result in unbalanced left subtree

  • @tanishqdadhich8589
    @tanishqdadhich8589 2 ปีที่แล้ว +46

    The time complexity of the first approach will be, N Log N, because if the tree is imbalanced, it'll have an early termination.
    If the tree is skewed it would return False very fast, so worst case is actually when the tree is balanced !

    • @parthsalat
      @parthsalat 2 ปีที่แล้ว +2

      💯💯💯

    • @only_for_fun1234r
      @only_for_fun1234r ปีที่แล้ว +1

      I didn't understand why very fast , its has to travel the complete height of left skew which will be (N) because we are traversing from root

    • @beingSenpai
      @beingSenpai ปีที่แล้ว +2

      what if the N = 10^8 and node left contains 10^8 -1 nodes and another one node at right side then we go through all the nodes on left side at last termination we come to know that its right after the root node is imblanced then it would taken almost O(N)

  • @PabitraPadhy
    @PabitraPadhy 3 ปีที่แล้ว +8

    If you follow the mycodeschool approach, where height of a null node is -1, then replace the exit condition from return 0 to return -1.
    also, replace all the -1 in the code to some flag value say, -2 or something in the negative side other than -1.
    you could also declare that flag as a static const inside the function.
    If this is done, then if it's balanced, you'll get directly the height of the node passed initially.
    Assuming, you're following height of a node, is the # of edges from from leaf to that node, in that branch.

  • @shashwatpriyadarshy7681
    @shashwatpriyadarshy7681 3 ปีที่แล้ว +107

    Correction : Instead of && (AND) in the first if-condition of lh & rh, it should be || (OR) .

    • @joseph2073
      @joseph2073 2 ปีที่แล้ว

    • @nopecharon
      @nopecharon 2 ปีที่แล้ว

      When will lh or rh becomes -1? And how?

    • @100mehulsingh8
      @100mehulsingh8 2 ปีที่แล้ว +4

      @@nopecharon Suppose, when we are calculating the max height of a subtree, it might not satisfy the balanced tree condition, so a height of -1 will be returned instead of the max height !!!

    • @maheshjindal2622
      @maheshjindal2622 2 ปีที่แล้ว

      @@nopecharon it'll become when the difference condition(if(abs(rh - lh))) will be executed

    • @maheshjindal2622
      @maheshjindal2622 2 ปีที่แล้ว

      he made a correction in the code!

  • @saicharangarapati2517
    @saicharangarapati2517 ปีที่แล้ว +4

    Thank You Striver❣
    By seeing 50% video I got the logic and I made it

  • @ruchivora1190
    @ruchivora1190 2 ปีที่แล้ว +11

    I struggled to understand the O(N^2) approach, but you made it so simple. Thank You!

  • @prakharmangal1152
    @prakharmangal1152 2 ปีที่แล้ว +11

    Python Solution:
    class Solution:
    def isBalanced(self, root: Optional[TreeNode]) -> bool:
    def check(node):
    if node == None:
    return 0
    lh = check(node.left)
    rh = check(node.right)
    if lh == -1 or rh == -1 or abs(lh-rh) > 1:
    return -1
    return 1 + max(lh, rh)
    return check(root) != -1

    • @VV-ps8rt
      @VV-ps8rt ปีที่แล้ว

      after calculating lh, you can immediately check if its -1 and return, will save lot of time especially if its right leaning tree

  • @adamyasharma_0135
    @adamyasharma_0135 10 หลายเดือนก่อน +2

    such a simple and easy to understand approach ,amazing

  • @tamannasharma3524
    @tamannasharma3524 หลายเดือนก่อน

    Really good explanation and the best part is converting the solution of calculating height to find if the tree is balanced is super..

  • @ayushkumar-wt2wm
    @ayushkumar-wt2wm 2 ปีที่แล้ว +5

    class Solution {
    public:
    bool res=true;
    int height_of_tree(TreeNode *root)
    {
    if(root==NULL)
    {
    return 0;
    }
    int left=height_of_tree(root->left);
    int right=height_of_tree(root->right);
    if(abs(left-right)>1)
    res=false;
    return max(left,right)+1;
    }
    bool isBalanced(TreeNode* root) {
    height_of_tree(root);
    return res;

    }
    };

    • @yashmundankar24
      @yashmundankar24 2 ปีที่แล้ว

      Did the same thing.
      class Solution {
      public:

      int func(TreeNode* root,bool &b){
      if(root==NULL){
      return 0;
      }
      int lh=func(root->left,b);
      int rh=func(root->right,b);
      if(abs(lh-rh)>1) b=false;

      return 1+max(rh,lh);

      }

      bool isBalanced(TreeNode* root) {
      if(root==NULL) return true;
      bool b=true;
      func(root,b);
      return b;

      }
      };

  • @lifehustlers164
    @lifehustlers164 ปีที่แล้ว +2

    Completed 16/54 (29% done) !!!

  • @AmanYadav-jf1yy
    @AmanYadav-jf1yy ปีที่แล้ว +8

    Using Ninja technique 😍😅😅
    take an extra variable ans, initially set ans=true and pass this variable to the btHeight(fxn to calculate height of binary tree) function by reference.
    If in function btHeight, abs(lh-rh)>1 is true then set ans=false.
    bool isBalanced(TreeNode* root) {
    if(root==nullptr)
    return true;
    bool ans=true;
    btHeight(root,ans);
    return ans;
    }
    int btHeight(TreeNode* root, bool &ans)
    {
    if(root==nullptr)
    return 0;
    int lh=btHeight(root->left, ans);
    int rh=(root->right, ans);
    if(abs(lh-rh)>1)
    ans=false;
    return 1+max(lh,rh);
    }

    • @pulkitjain5159
      @pulkitjain5159 ปีที่แล้ว +2

      yup , i did it too , but your code is still doing extra calculations ,just do a return when left or right has a false and at end you will find that your logic and striver's logic are same

    • @udayptp
      @udayptp ปีที่แล้ว

      Noice

  • @ishangujarathi10
    @ishangujarathi10 ปีที่แล้ว +1

    AMazing intuition, understood completely through dry run

  • @kakadiazeel
    @kakadiazeel 3 ปีที่แล้ว +8

    class Solution {
    boolean isBal=true;
    public boolean isBalanced(TreeNode root) {
    helper(root);
    return isBal;
    }
    public int helper(TreeNode root){
    if(root==null)
    return 0;
    int left = helper(root.left);
    int right = helper(root.right);
    int gap = Math.abs(left-right);
    if(gap>1)
    isBal=false;
    int height = Math.max(left,right)+1;
    return height;
    }
    }

  • @stith_pragya
    @stith_pragya ปีที่แล้ว +1

    Thank You So Much for this wonderful video...............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @bhavkushwaha
    @bhavkushwaha 7 หลายเดือนก่อน

    Thankyou Striver, Understood!

  • @engineervinay
    @engineervinay ปีที่แล้ว

    i decided to solve this problem on my own first and after solving thought that i should see the optimized approach and when i started watching i realized my solution is exactly same as striver's
    this was my solution:
    int getheight(TreeNode* root){
    if(root==NULL)return 0;
    int lh=getheight(root->left);
    int rh=getheight(root->right);
    if(lh==-1||rh==-1)return -1;
    if(abs(lh-rh)>1)return -1;
    return 1+max(lh,rh);
    }
    bool isBalanced(TreeNode* root) {
    if(getheight(root)==-1)return false;
    return true;
    }
    i don't know how i got this solution and first time so surprised that i got exact same solution as him i saw the previous problems solution so i knew we just have to make changes in that code.

  • @nagame859
    @nagame859 ปีที่แล้ว

    Understood! By the way... the comments section of your videos is quite good👍. Want to say this since a very long time..

  • @utsavseth6573
    @utsavseth6573 ปีที่แล้ว +3

    Me solving a question on my own by some magic once in a bluemoon.
    Le striver: So the naive approach is.....turns out to be my approach.....
    I am like ...damn, this guy....
    😂😂😂😂😂.

  • @ritikshandilya7075
    @ritikshandilya7075 6 หลายเดือนก่อน +1

    Thankyou so much Striver ,you are the best

  • @PrashantSingh-qr3vn
    @PrashantSingh-qr3vn 9 หลายเดือนก่อน +26

    I have a doubt why a person of your caliber is working for some MNC when he can easily build some million dollar business since you are the person who can get things done

    • @DrOnZeR2022
      @DrOnZeR2022 3 หลายเดือนก่อน +3

      Striver does not want to sell paid courser he wants to help students in this way and I think striver loves his profession that's why he is not building his bussines

    • @mysterious5729
      @mysterious5729 2 หลายเดือนก่อน +3

      ​@@DrOnZeR2022 surprise!
      Course launched ✨

    • @DrOnZeR2022
      @DrOnZeR2022 2 หลายเดือนก่อน +1

      @@mysterious5729 🤣🤣 this thing he said 2 year before in a vedio

  • @subhajitdey135
    @subhajitdey135 2 หลายเดือนก่อน +1

    We can use the brute force code with memoization also :
    unordered_map st;
    int height(TreeNode* node) {
    if (!node)
    return 0;
    if(st.find(node)!=st.end()) return st[node];
    int l = height(node->left);
    int r = height(node->right);
    return st[node]=1 + max(l, r);
    }
    bool isBalanced(TreeNode* root) {
    if (!root)
    return true;
    int l=height(root->left);
    int r=height(root->right);
    if (abs(l - r) > 1)
    return false;
    bool check1 = isBalanced(root->left);
    bool check2 = isBalanced(root->right);
    if (!check1 || !check2)
    return false;
    return true;
    }

  • @TheWierdVibe
    @TheWierdVibe 2 ปีที่แล้ว +3

    Everything becomes easy when it's striver!

  • @sankalpjain4841
    @sankalpjain4841 3 ปีที่แล้ว +9

    It should also find the right height of node 1 right?
    We need to visit the right subtree of node 1 also right?
    At 11.46 we returned -1 directly after we got lh==-1 before we got the rh.

    • @anujaditya02
      @anujaditya02 2 ปีที่แล้ว

      As said by striver, even if we encounter -1 on the left side we are gonna return -1 as we are explicitly specifying that if left return -1 then the whole function return -1.

    • @_nucleus
      @_nucleus 2 ปีที่แล้ว +6

      Yes I am also confused for the same,
      if(lh == -1 || rh == -1) return -1;
      This above statement can only run after,
      rh = check(node->right);
      And since in function call we will pass the root, how rh = check(node->right); this can be skipped, it will check for right child of root but both the codes on 12:00 is right because there he is checking before making call to right

    • @rishabhnegi9806
      @rishabhnegi9806 2 ปีที่แล้ว +5

      @@_nucleus yeah u are right ... even if we have lh as -1, the recursion will still chk for right tree as before calling for root->right we aren't checking whether we got any -1 till this point ...
      so in my opinion we can just chk if( lh==-1 ) before calling for rh .

    • @prakhar_pratyush
      @prakhar_pratyush 2 ปีที่แล้ว +1

      @@rishabhnegi9806 yes I submitted both the approaches on leetcode. Putting lh==-1 before calling for rh, reduces the runtime by half.

    • @CodePinaka
      @CodePinaka 2 ปีที่แล้ว

      was looking for the same doubt in the comment section.... thanks vro

  • @CuriousAyushi
    @CuriousAyushi 2 หลายเดือนก่อน

    What an Explanation MANNN!!!!!!!!!!!!!!! 💫💯

  • @apmotivationakashparmar722
    @apmotivationakashparmar722 26 วันที่ผ่านมา

    Thank you So much Striver !

  • @muktarsayeed9198
    @muktarsayeed9198 ปีที่แล้ว +1

    Good explanation. Walking through the diagram with the code is particularly useful 👍👍👍

  • @vasudhajha9115
    @vasudhajha9115 2 ปีที่แล้ว +2

    Another wonderful explanation + solution! Thank you for demystifying trees!

  • @viditsrivastava1747
    @viditsrivastava1747 2 หลายเดือนก่อน

    Out of the box solution!🤯

  • @nawabkhan4916
    @nawabkhan4916 2 ปีที่แล้ว +1

    great work, and have lots of sponsor ad so that you can provide great videos.

  • @firebout7675
    @firebout7675 9 หลายเดือนก่อน

    bit manipulation playlist pls....thanks for this wonderful video

  • @noob_coder4017
    @noob_coder4017 4 หลายเดือนก่อน

    Tq striver❤for this wonderful playlist

  • @rohandevaki4349
    @rohandevaki4349 2 ปีที่แล้ว +1

    at 1:23 , it is abs( height(left) - height(right))

    • @omkumar4211
      @omkumar4211 ปีที่แล้ว

      Yaa that's correct, that's the required condition for a balanced 🌲

  • @uRamPlus
    @uRamPlus 3 ปีที่แล้ว +3

    this channel is a hidden gem 💎! thank you

  • @ganeshjaggineni4097
    @ganeshjaggineni4097 3 หลายเดือนก่อน

    NICE SUPER EXCELLENT MOTIVATED

  • @akashverma5756
    @akashverma5756 4 หลายเดือนก่อน

    I call this method of solving Recursion Exploitation. We design recursion for different purpose but exploit every recursive call separately to find our answer.

  • @studyonly9622
    @studyonly9622 4 หลายเดือนก่อน

    I think you missed the root->right tree steps.
    or put that condition right after lh=check(node->left)

  • @itzmartin20
    @itzmartin20 11 หลายเดือนก่อน

    love your dedication! understood sir!

  • @utkarsh_108
    @utkarsh_108 2 ปีที่แล้ว

    IDK why but your brute force approach is giving WA for other TCs on Leetcode

  • @shameemahamad2617
    @shameemahamad2617 ปีที่แล้ว

    Very nice series to under all types of scenarios what can come across from binary tree.

  • @stith_pragya
    @stith_pragya ปีที่แล้ว

    Ye dekh k bohot acha lgta h jb you come on to some video of a pro coder to check for a better solution than yours and you find it exactly the same..........🙂

  • @codeman3828
    @codeman3828 7 หลายเดือนก่อน

    Great eexplanation

  • @Sakthivel-zq1hb
    @Sakthivel-zq1hb 2 หลายเดือนก่อน

    I have a doubt Striver Sir,
    [1,2,3,4,5,6,null,8] -- this is balanced based on the formula ((lh - rh)

  • @Aditya-wy4ci
    @Aditya-wy4ci 21 วันที่ผ่านมา

    But for node 1,recursive call will be made for right subtree and then it will return -1

  • @himanshugupta7010
    @himanshugupta7010 2 ปีที่แล้ว +1

    Thanks Striver for such an amazing explanation . never ever thought this question like this . you completely changed my thought process. 🙌

  • @deepakjain4481
    @deepakjain4481 ปีที่แล้ว

    maybe someday i will be called as a genius despite of my horrible failures i will try i will keep going someday i will compete with my dream

  • @animeshsrivastava9938
    @animeshsrivastava9938 3 ปีที่แล้ว

    class Solution {
    public boolean isBalanced(TreeNode root) {
    return height(root) != -1;
    }
    int height (TreeNode root) {
    if (root == null) return 0;
    int leftH = height(root.left), rightH = height(root.right);
    if ((rightH == -1) || ((leftH == -1)) || (Math.abs(leftH - rightH) > 1)) return -1;
    else return Math.max(leftH, rightH) + 1;
    }
    }

  • @adebisisheriff159
    @adebisisheriff159 9 หลายเดือนก่อน

    Thanks alot Striver!!!

  • @lsdxsaurabh2798
    @lsdxsaurabh2798 2 ปีที่แล้ว

    IN find height function base condition should return -1 .
    otherwise function will return height + 1 .

    • @rhl_
      @rhl_ 2 ปีที่แล้ว

      There exist variety of definiton of height and depth of a tree.
      Some count nodes while some count egdes depends on the problemsetter.
      In this case it was mentioned in the question what height here is meant.
      good day :)

  • @ajaykushwaha6137
    @ajaykushwaha6137 3 ปีที่แล้ว +13

    Hey Buddy, thanks for such an awesome Video, but can u give an example of when the case will be lh==-1 or rh==-1? I couldn't understand.

    • @HimanshuSingh-zu4ht
      @HimanshuSingh-zu4ht 3 ปีที่แล้ว +20

      just happen in the same video example brother
      lh become -1 after if(abs(lh-rh)>1) return -1 now again when come to the check fuction here lh =-1 and if(lh==-1or rh==-1) return -1 and then we come out of check function with -1

    • @amanbhadani8840
      @amanbhadani8840 2 ปีที่แล้ว +2

      Just do the dry run once on copy,u will understand better.

  • @KartikeyTT
    @KartikeyTT 3 หลายเดือนก่อน

    tysm sir

  • @thatowlfromhogwarts490
    @thatowlfromhogwarts490 2 ปีที่แล้ว +1

    int is_balanced(Node root) {
    if(root == NULL) return 0;
    int lh = is_balanced(root->l);
    if(lh == -1) return -1;
    int rh = is_balanced(root->r);
    if(rh == -1) return -1;
    if(abs(lh-rh)>1) return -1;
    return 1+max(lh, rh);
    }

  • @adityapandey23
    @adityapandey23 หลายเดือนก่อน

    Understood

  • @sauravchandra10
    @sauravchandra10 ปีที่แล้ว

    Mazedaar approach

  • @yashpaunikar671
    @yashpaunikar671 8 หลายเดือนก่อน

    loved it...!!!🤩🤩

  • @devanshrai8972
    @devanshrai8972 ปีที่แล้ว +6

    my approach::
    int recur(TreeNode* root, int* ans)
    {
    if(root==NULL) return 0;
    int l = recur(root->left,ans);
    int r = recur(root->right,ans);
    if(abs(l-r)>1) *ans=0;
    return max(l,r)+1;
    }
    bool isBalanced(TreeNode* root)
    {
    int ans = 1;
    recur(root,&ans);
    if(ans==0)
    return false;
    else return true;
    }

  • @RITIKSINGH-re5ne
    @RITIKSINGH-re5ne 2 ปีที่แล้ว

    Check for Balanced Binary Tree Solution in JAVA:
    public boolean isBalanced(TreeNode root) {
    return height(root) != -1;
    }
    public int height(TreeNode root) {
    if(root == null ) return 0;
    int left = height( root.left );
    if( left == -1 ) return -1;
    int right = height( root.right );
    if( right == -1 ) return -1;
    if( Math.abs( left - right ) > 1 ) return -1;
    return Math.max( left, right ) +1;
    }

  • @tanveer.shaikh
    @tanveer.shaikh 2 ปีที่แล้ว

    instead of using we can use boolean array like you did in diameter

  • @Shivi32590
    @Shivi32590 4 หลายเดือนก่อน

    thank you

  • @deepanshurohilla2114
    @deepanshurohilla2114 ปีที่แล้ว +1

    Bhaiya at 11:02 I don't think so that -1 will be returned earlier before visiting the right portion.
    right recursive calls will be done before returning -1 as answer
    Please correct em if i am wrong

    • @deepanshurohilla2114
      @deepanshurohilla2114 ปีที่แล้ว

      sorry my bad i wrote this comment only based on the pseudo code
      at 11:15 its fine now 😂😂😂😂

  • @tushar5359
    @tushar5359 ปีที่แล้ว

    Thanks Striver!, I learnt a lot from you!

  • @dakshchandore6435
    @dakshchandore6435 2 หลายเดือนก่อน +1

    how can the height be -1 since the height can either be zero or >zero and i didn't understand this line
    if(leftHieght == -1 ) return -1 and if (rightHeight == -1 ) return -1 ;

  • @eshanchourasia287
    @eshanchourasia287 3 ปีที่แล้ว

    Bhaiya ek Sara imp algorithm ka bhi video bna dona pls .aapka hi smgh aata hai adat lk gya hai😁

  • @mriduljain1981
    @mriduljain1981 ปีที่แล้ว

    completed lecture 15 of Tree playlist.

  • @annaswaheed2615
    @annaswaheed2615 2 ปีที่แล้ว +1

    best explanation so far 💯!!!

  • @niranjanraje7160
    @niranjanraje7160 3 ปีที่แล้ว +4

    In the first approach, we keep checking for each and every node if its left subtree and right subtree height

  • @Anand-zg6jv
    @Anand-zg6jv 2 ปีที่แล้ว

    thanxx . i was stuck at this question for last two days ...♥

  • @subee128
    @subee128 ปีที่แล้ว

    Thanks

  • @Drizilingfacts
    @Drizilingfacts 2 ปีที่แล้ว

    We can also use map here to keep heights of trees

  • @tiger7858
    @tiger7858 ปีที่แล้ว

    function isBalanced (root) {
    return height(root) ;
    function height(node){
    if (!node) return true
    let left = height(node.left), right = height(node.right);
    return ( !left || !right || Math.abs(left - right) > 1 ) ? false : 1 + Math.max(left, right);
    }

    };

  • @gigglezone3432
    @gigglezone3432 ปีที่แล้ว +1

    Can we instead have a global variable, flag and if at any moment left-right subtree height not

  • @ayushmishra1207
    @ayushmishra1207 ปีที่แล้ว +1

    Bhaiya In which case the height of left or right side will -1 ??

  • @cinime
    @cinime 2 ปีที่แล้ว

    Understood! Such a smart explanation as always, thank you very much!!

  • @abhishekverma332
    @abhishekverma332 3 ปีที่แล้ว +4

    Actually, while submitting on Leetcode the latter solution (approach 2) is faster than 64.02% of C++ online submissions although being O(n) whereas the earlier is faster than 90.42% of C++ submission though it being O(n^2). I am actually not getting this, or I am missing anything. Kindly help!

    • @parthapratimghose173
      @parthapratimghose173 3 ปีที่แล้ว

      i submitted by myself something si,miliar to the 2nd i got 99% best 4ms solution some extra recursion you have used ?

    • @Usurperhk
      @Usurperhk 3 ปีที่แล้ว +16

      Leetcode's runtime is a scam. Focus on the time complexity of code you write.

    • @KrishnaSharma-bv5ys
      @KrishnaSharma-bv5ys 2 ปีที่แล้ว

      Please show the code of 1st approach

    • @matrixtoogood5601
      @matrixtoogood5601 2 ปีที่แล้ว +4

      Leetcode test cases might not be of very big trees with billions of nodes. In smaller examples, time complexity will not explain the running/execution time accurately since asymptotic analysis assumes almost infinitely large sizes.
      For smaller test cases, execution time will depend on network latency, CPU thread switching etc. which is pretty non-deterministic

    • @tanishqdadhich8589
      @tanishqdadhich8589 2 ปีที่แล้ว

      @@matrixtoogood5601 Sounds like avalid point!

  • @gangsta_coder_12
    @gangsta_coder_12 3 ปีที่แล้ว +2

    Excellent explanation as always 🔥🔥🔥🔥👌👌👌👌

  • @codding32world50
    @codding32world50 2 ปีที่แล้ว +1

    Thank You Striver:)))))))))))))))))))))))))))))))

  • @culeforever5408
    @culeforever5408 9 หลายเดือนก่อน

    understood

  • @shivam4you744
    @shivam4you744 3 ปีที่แล้ว +1

    I understood the code and entire explanation : ) 🔥🔥🔥🔥👌👌👌👌

  • @AnkitKumar-jm3cz
    @AnkitKumar-jm3cz 2 ปีที่แล้ว

    loved your optimised approach bhaiya

  • @pratyakshhhhhhhhhhhhhhhhhhhhh
    @pratyakshhhhhhhhhhhhhhhhhhhhh ปีที่แล้ว

    Yeahhh❤❤❤

  • @Nikita-hv5jm
    @Nikita-hv5jm 2 ปีที่แล้ว +1

    thaks a lot for such a great explanation !!

  • @nuraynasirzade
    @nuraynasirzade 10 หลายเดือนก่อน

    good !

  • @KeepCalm__
    @KeepCalm__ ปีที่แล้ว

    @10:26 correction: height of the tree is 1when stand at 3

  • @jasonbrody4618
    @jasonbrody4618 3 ปีที่แล้ว +1

    Liked. Cfbr will watch after some time

  • @rydmerlin
    @rydmerlin 6 หลายเดือนก่อน

    In this video you used && when you said ‘or’

  • @abhaykumarsingh3884
    @abhaykumarsingh3884 2 หลายเดือนก่อน

    I solved this in O(N) but using this approach
    class Solution {
    class Pair{
    boolean condn;
    int num;
    Pair(boolean condn,int num){
    this.condn=condn;
    this.num=num;
    }
    }
    public Pair getAns(TreeNode root){
    if(root==null){
    return new Pair(true,0);
    }
    Pair lh=getAns(root.left);
    Pair rh=getAns(root.right);
    if(Math.abs(lh.num-rh.num)>1 || !lh.condn || !rh.condn){
    return new Pair(false,1+Math.max(lh.num,rh.num));
    }
    return new Pair(true,1+Math.max(lh.num,rh.num));
    }
    public boolean isBalanced(TreeNode root) {
    return getAns(root).condn;
    }
    }

  • @rishabhkesarwani5761
    @rishabhkesarwani5761 3 ปีที่แล้ว +4

    One more Solution using the pair that contains both height and isBalance . T.C. -> O(N)
    class Solution {
    class pair{
    int height;
    boolean isbal;
    pair(int height,boolean isbal){
    this.height=height;
    this.isbal=isbal;
    }
    pair(){};
    }
    public boolean isBalanced(TreeNode root) {
    pair ans = isBalanceHelper(root);
    return ans.isbal;
    }

    public pair isBalanceHelper(TreeNode root){
    if(root==null){
    pair ans=new pair(0,true);
    return ans;
    }

    pair left=isBalanceHelper(root.left);
    pair right=isBalanceHelper(root.right);

    pair ans=new pair();
    ans.height=1+Math.max(left.height,right.height);
    ans.isbal=true;
    if(!left.isbal||!right.isbal){
    ans.isbal=false;
    }

    if(Math.abs(left.height-right.height)>1){
    ans.isbal=false;
    }
    return ans;

    }
    }

  • @subhajitdey135
    @subhajitdey135 2 หลายเดือนก่อน

    How is the time complexity of the brute force solution O(n*n) and not O(2*n). Dont the recursion calls of the findH finishes first and then for the function check() starts ? I know that this recursion has overlapping sub problems. But why is the time complexity O(n*n) for the overall brute force

  • @rohandevaki4349
    @rohandevaki4349 2 ปีที่แล้ว

    at 4:47 , why did you multiply the time complexities of left and right? why dont we add them?

  • @subramanir8438
    @subramanir8438 4 หลายเดือนก่อน

    10:18 here why is it fine if lh-rh is equal to 1. Still it's an unbalanced tree right. Wondering why greater than 1 is taken instead of greater than or equal to 1

  • @saketnit-silchar276
    @saketnit-silchar276 9 หลายเดือนก่อน

  • @stark2461
    @stark2461 ปีที่แล้ว +1

    This code won't work when tree is left skewed or right skewed. change return 0 to -1 when root == NULL and change others as -2

  • @saikirank6357
    @saikirank6357 ปีที่แล้ว +1

    In what case either left height or right height is equal to -1?

    • @omkumar4211
      @omkumar4211 ปีที่แล้ว

      Same qn

    • @taqimustafa7665
      @taqimustafa7665 7 หลายเดือนก่อน

      when either of the subtree would be unbalanced,the previous function call would have returned -1 to them,so if unbalancing is found,we dont need to check further

  • @surbhiranjan7808
    @surbhiranjan7808 3 ปีที่แล้ว +2

    When does this condition occur? if (rightHeight == -1) return -1; if (leftHeight == -1) return -1;

    • @praveenrajs94
      @praveenrajs94 3 ปีที่แล้ว +7

      the line lh = check(root->left) means that check whether the left subtree is balanced or not, and if that function returns -1 (which is stored in the lh variable), it means that the left subtree is not balanced. same is the condition with right subtree, if either of them turn out to be unbalanced then the entire tree is unbalanced.

    • @lakshsinghania
      @lakshsinghania ปีที่แล้ว +1

      @@praveenrajs94 thnks for the explanation! i was looking for the same only

  • @RaviYadav-rr3up
    @RaviYadav-rr3up 2 ปีที่แล้ว

    In the introductory video you told that balanced tree is one whose heigh is at max log(N) , why cant we use that concept here ??

  • @souravsarkar6107
    @souravsarkar6107 3 ปีที่แล้ว

    thank you for teaching

  • @rahulsbhatt
    @rahulsbhatt ปีที่แล้ว

    Such an excellent video!
    Thanks, Striver Bhai!