L32. Count total Nodes in a COMPLETE Binary Tree | O(Log^2 N) Approach | C++ | Java

  Рет қаралды 110,725

take U forward

take U forward

Күн бұрын

Entire DSA Course: takeuforward.org/strivers-a2z...
Check our Website:
Linkedin/Instagram/Telegram: linktr.ee/takeUforward
#treeSeries #striver #placements

Пікірлер: 245
@takeUforward
@takeUforward 2 жыл бұрын
Please likeeee, shareeee and subscribeeeeeeee :) Also follow me at Insta: Striver_79
@bhaveshkumar6842
@bhaveshkumar6842 2 жыл бұрын
Why is it return (1
@g.upenderreddy
@g.upenderreddy Жыл бұрын
@@bhaveshkumar6842 If you observe carefully there's change in height calculation root vs root.left or root.right public int countNodes(TreeNode root) { if (root == null) return 0; int leftHeight = leftHeight(root); int rightHeight = rightHeight(root); if (leftHeight == rightHeight) return (1
@_PRANAYMATE
@_PRANAYMATE 5 ай бұрын
@@g.upenderreddy You are right
@abhishekanand5898
@abhishekanand5898 2 жыл бұрын
This question was asked today in DUNZO round-1 and interviewer was expecting this approach
@uRamPlus
@uRamPlus 2 жыл бұрын
Self Notes: 🍇 Formula is (2^TreeLevel - 1). Only works for perfect tree. 🍇 To determine if its a perfect tree, go all the way down and count the nodes on left and right side, If they match, its a perfect tree and our formula applies. 🍇 If its not a perfect tree, we go on left and right subtree and again determine if they are perfect tree. 🍇 When we have our left and right heights, we do (1 + left + right) 🍇 If we reach null, return 0 🍇 C++ note: 1
@MeenakshiSharma-bd3bu
@MeenakshiSharma-bd3bu 2 жыл бұрын
Thankyou for posting, this also helps in making a short and crisp notes!!
@gandhijainamgunvantkumar6783
@gandhijainamgunvantkumar6783 2 жыл бұрын
Thanks for posting this I am also using your notes.
@shikharbansal2942
@shikharbansal2942 2 жыл бұрын
Keep posting!
@shaikfahim1232
@shaikfahim1232 Жыл бұрын
Shukriya bhai
@parthsalat
@parthsalat Жыл бұрын
The emoji's are lit 🔥
@bhaveshkumar6842
@bhaveshkumar6842 2 жыл бұрын
Thank you so much Striver for your amazing content. You have helped me understand the concepts which I initially used to find too intimidating.
@dhirenparekh2646
@dhirenparekh2646 2 жыл бұрын
Instead of using two function to find left height first and then right height. We can use a single while loop and iterate till both of the root are not NULL and increment h++. if both are NULL then make the bool full=true and break and if anyone of them is NULL then just break. if full is true then return 2^h-1 else return 1+left+right int countNodes(TreeNode* root) { if(root==NULL)return 0; TreeNode* node=root->left; TreeNode* node2=root->right; int h=1; bool full=false; while(1){ if(node==NULL && node2==NULL){ full=true;break; } if(node==NULL || node2==NULL){ break; } node=node->left,node2=node2->right; h++; } if(full){ return pow(2,h)-1; } else{ return 1+countNodes(root->left)+countNodes(root->right); } }
@sakshigupta7616
@sakshigupta7616 2 ай бұрын
Loved the video. You are the best! Keep going...
@naveenkumarjadi2915
@naveenkumarjadi2915 5 ай бұрын
Great Explanation bro , thank you for this series
@stith_pragya
@stith_pragya 8 ай бұрын
Thank You So Much for this wonderful video...........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@paragroy5359
@paragroy5359 2 жыл бұрын
Nice explanation your videos are really good...please keep on making such videos...you are doing a great job.
@cinime
@cinime Жыл бұрын
Understood! So fantastic explanation as always, thank you very much!!
@iampatelajeet
@iampatelajeet 2 жыл бұрын
Leetcode discussion section se pareshaan hoke aaya and you satisfied bro.. thank youuuuuu
@rishabhkumar8115
@rishabhkumar8115 2 жыл бұрын
Great explanation & crisp code just what striver do
@vedbhanushali608
@vedbhanushali608 10 ай бұрын
thank you sir great explanation.
@arijitdey8419
@arijitdey8419 2 жыл бұрын
great explanation as expected..thanks a lot for ur efforts
@falgunitagadkar4097
@falgunitagadkar4097 Жыл бұрын
I went on to like this video when Striver asked for it at the end but found out that I already liked it (this is the first time I am watching this video). This is magic for Striver!!!
@MilindBasavaraja123
@MilindBasavaraja123 Жыл бұрын
Awesome Explanation. Leetcode solution was intimidating.
@harisrashid0773
@harisrashid0773 2 жыл бұрын
Loved it solved on my own
@jayasuryam3976
@jayasuryam3976 2 жыл бұрын
If anyone struck here , see this (2
@AvikNayak_
@AvikNayak_ Жыл бұрын
sukriya!!!
@adebisisheriff159
@adebisisheriff159 6 ай бұрын
Thanks Striver
@harisrashid0773
@harisrashid0773 2 жыл бұрын
Rather than finding height at every node we can pass left height and right height and in left recursion left height is equal to left height -1 and give call for right height and in right subtree right height is right height -1 and give call for left height only.
@vrandakansal5940
@vrandakansal5940 2 жыл бұрын
What a explanation...🙇‍♀️thanks a lot😅👍
@KalingaAbhisek
@KalingaAbhisek Жыл бұрын
Just a correction to striver bhaiya's java code class Solution { public int countNodes(TreeNode root) { if(root==null) return 0; int left=ghl(root.left); int right=ghr(root.right); if(left==right) return (2
@konpeeyush
@konpeeyush Жыл бұрын
Thanks bro
@mdrehankhan3458
@mdrehankhan3458 Жыл бұрын
it should be (1
@deepanshuaggarwal5181
@deepanshuaggarwal5181 Жыл бұрын
@@mdrehankhan3458 yes
@Tanishq181
@Tanishq181 7 ай бұрын
Niceeee
@shastriamisha
@shastriamisha 2 жыл бұрын
Great Great Explanation..Thank you
@bhaveshkumar6842
@bhaveshkumar6842 2 жыл бұрын
Why is it return (1
@mayanksaurabhmayanksaurabh9271
@mayanksaurabhmayanksaurabh9271 Жыл бұрын
loved the thought process. Dry running it multiple really helped me grasp the concept. Good question using the properties of a specific type of tree.
@user-tk2vg5jt3l
@user-tk2vg5jt3l 4 ай бұрын
Thank you Bhaiya
@manasranjanmahapatra3729
@manasranjanmahapatra3729 Жыл бұрын
This nice intuition really liked it.
@aryanyadav3926
@aryanyadav3926 2 жыл бұрын
Amazing video!
@rishabhkumar8115
@rishabhkumar8115 2 жыл бұрын
Great explanation & crisp code just what striver do Great explanation & crisp code just what striver do
@nagavedareddy5891
@nagavedareddy5891 2 жыл бұрын
Huge Respect...❤👏
@gouravkushwaha3001
@gouravkushwaha3001 10 ай бұрын
My intuition before watching this video is normal traversing the tree from like BFS and while traversing take the count variable and increase the count by 1 if any node occurs while traversing the tree and then return count
@TarunKumar-cn6in
@TarunKumar-cn6in 2 жыл бұрын
Thanks so much sir🙏
@prateekjain7623
@prateekjain7623 2 жыл бұрын
you are superb sir
@vrandakansal5940
@vrandakansal5940 2 жыл бұрын
Greatt explanation....🙏🙏
@ishanshah3309
@ishanshah3309 2 жыл бұрын
Very good explanation. I specially loved the explanation of Time complexity for this. I think no one clears TC in their videos apart from striver. so this clearing Time space complexities and clear explanation of solution gives him edge and makes him better than anyone teaching DSA
@parthsalat
@parthsalat Жыл бұрын
It's uplifting to see children learn DSA at such a young age...
@lavanyaprakashjampana933
@lavanyaprakashjampana933 Жыл бұрын
we love your content and we love you...🖤
@laveshgarg2189
@laveshgarg2189 2 жыл бұрын
Great approach bhaiya maza aa gaya
@krishnavamsichinnapareddy
@krishnavamsichinnapareddy 2 жыл бұрын
Understood 👍 Time complexity explanation is really good
@kavatishivakumar1733
@kavatishivakumar1733 Жыл бұрын
can u explain why time complexity logn^2?
@AdityaKumar-be7hx
@AdityaKumar-be7hx Жыл бұрын
Buddy, you are amazing!
@rishabhgupta9846
@rishabhgupta9846 Жыл бұрын
understood,awesome striver
@rishabkumar4940
@rishabkumar4940 2 жыл бұрын
This can also be done by binary searching on all the sizes possible. We have to check whether the size element is present in the tree or not. The complexity will be the same.
@anshumansharma4580
@anshumansharma4580 2 жыл бұрын
This can also be done by applying binary search in the last level. Getting to the last level will take logN time and binary Search itself will take logN time, so overall logN_wholeSquare time
@your_name96
@your_name96 2 жыл бұрын
checking whether the size element is present or not takes O(n) time for binary tree right?
@sparshsharma6068
@sparshsharma6068 2 жыл бұрын
Bhaiya, there is a typo in the java code, the while loop will have to execute till root != null but, in the code there was root.left and root.right in the two functions(We can also set the count initial value to 1, that will also work). At 13:17 , I got a bit confused on the part when you were explaining the calculation of the left height and the right height. I thought that the code won't work for the test case : 1 / \ 2 3 / \ / \ 4 5 6 7 / \ / 8 9 10 But then I dry ran the code and got to know that what you are actually doing is finding the depths of the rightmost and leftmost nodes of a given root. And then if you find that the depths are equal, we can thereby conclude that for the given root, the leftmost and the rightmost nodes are at the same level, and thus, by the definition of complete binary trees, we can further conclude that the tree is completely filled the complete binary tree. So, for the test case I mentioned above, for node 2, the depth of leftmost node (8) != the depth of rightmost node(5). So it will go on and check for the individual deeper nodes. Thus, the code will work fine.
@kaushikramabhotla4635
@kaushikramabhotla4635 2 жыл бұрын
concluded well :)
@sparshsharma6068
@sparshsharma6068 2 жыл бұрын
@@kaushikramabhotla4635 Thanks 😊
@naveen9646
@naveen9646 Жыл бұрын
thanks clearing my doubt
@ishantrivedi5588
@ishantrivedi5588 11 ай бұрын
what if the tree is like this: 2 / \ 3 4 / \ / \ 3 5 9 10 / 6 then for left subtree of 2's height will be 3 and for right subtree of 2 it will also be 3 then it will calculate the nodes with the help of that formula which will give wrong result since it missed the last node (6).
@BharatTheGreat1
@BharatTheGreat1 10 ай бұрын
@@ishantrivedi5588 this is not a complete binary tree according to question given questions is always a complete binary tree
@isheep9025
@isheep9025 Жыл бұрын
Self note: why complexity is log2n, except for the last level every level needs to be compelty filled, so in the left side we might have to go till the last level finding a node whose left height and right height are similar which can be at max till approx logn level since complete is full execpt the last level ,and in every of those logn levels we came we must have computed height each of logn (since complete tree is filled execpt the last level) ->ogn*logn
@aryanraj3413
@aryanraj3413 Жыл бұрын
didn't understand why complexity is log2n
@jk-sm6qr
@jk-sm6qr 4 ай бұрын
thanks
@hapysethi1306
@hapysethi1306 Жыл бұрын
Thanks brother!
@chiragbansod8252
@chiragbansod8252 5 ай бұрын
UNDERSTOOD
@gowreeManohar
@gowreeManohar 2 жыл бұрын
got it sir thanks a lot
@alesblaze4745
@alesblaze4745 2 жыл бұрын
thanks mate!
@prajjwaldeepghosh7329
@prajjwaldeepghosh7329 Жыл бұрын
Thank you
@DurgaShiva7574
@DurgaShiva7574 2 жыл бұрын
Mauj kardi bhai ne.. 🔥🔥
@budgetdapr
@budgetdapr Жыл бұрын
Hi, Considering the subtrees of Node 2, if we remove the Node 11 from the right sub-tree, The height would still be 3. Am I getting it wrong?
@subhasishkumbhakar1028
@subhasishkumbhakar1028 7 ай бұрын
NO, you are wrong. If we remove 11, it will not be same.
@UECAshutoshKumar
@UECAshutoshKumar Жыл бұрын
Thank you sir
@harshitjaiswal9439
@harshitjaiswal9439 5 ай бұрын
understood
@arpanbanejee5143
@arpanbanejee5143 2 жыл бұрын
I could not understand how the TC Is O(logn^2) for the worst-case. even if we apply the formula, for calculating the left and right height we are traversing through all the nodes for the right part. Someone pls help me.
@takeUforward
@takeUforward 2 жыл бұрын
Complete binary tree height is always log n.
@pranavkorhale5089
@pranavkorhale5089 Жыл бұрын
I have a small query in java code. when you call the getHeightRight or getHeightLeft Function why do you write while(root. left!=null) --It does not give actual height of the left side why not like this while(root!=null) -- it gives correct height //your code is working fine. //above query ate my head.
@aasthagoyal5059
@aasthagoyal5059 2 жыл бұрын
isme hm root k left me jakr uski height calculate krte, fr height-1 tk perfect tree hi hoga to (2^height-1) krke, ek level km tk ki nodes aajati or fr last level k liye root .right use krke nodes count krte or fr dono ko add ......ese nhi krskte kya?? static int height= 0; static int sum= 0; public int countNodes2(TreeNode root){ TreeNode lastNode = countLevel(root); sum += (( 2
@oqant0424
@oqant0424 Жыл бұрын
understood!!!!!!!!!!!!!
@aanchalmittal9897
@aanchalmittal9897 2 жыл бұрын
But in worst case as you explained each time we traverse to left we will also go to right part.. So how is it that we will traverse logn nodes?
@AmanKumar-dh8md
@AmanKumar-dh8md Жыл бұрын
Doesn't this traverse all the nodes while calculating height?
@stephen9726
@stephen9726 Жыл бұрын
Nope. While finding height we just keep on going on one side. If we're finding left height we just keep on going left till we find a NULL and we don't move to right.
@dipanshut
@dipanshut 2 жыл бұрын
Understood!!
@adityasaxena6971
@adityasaxena6971 Жыл бұрын
Great sirr
@oqant0424
@oqant0424 Жыл бұрын
just a correction to bhaiya's cpp code: class Solution { private: int findHeightLeft(TreeNode* node){ int hgt=0; while(node){ hgt++; node=node->left; } return hgt; } int findHeightRight(TreeNode* node){ int hgt=0; while(node){ hgt++; node=node->right; } return hgt; } public: int countNodes(TreeNode* root) { if(!root)return NULL; int lh=findHeightLeft(root->left); int rh=findHeightRight(root->right); if(lh==rh)return (1right); } };
@avyavsharma2994
@avyavsharma2994 Жыл бұрын
bro you're a savior been trying figure out for so long what was wrong with the code thanxxxxxx
@vardankaushik3941
@vardankaushik3941 7 ай бұрын
In case we have 10 node, won't this approach will traverse all the node to find the solution?
@deepaksarvepalli2344
@deepaksarvepalli2344 2 жыл бұрын
Is it guaranteed that the left height and right height are equal they are completely filled?
@takeUforward
@takeUforward 2 жыл бұрын
That is the definition of complete tree, since you going till the right most point to find the right height, so if the right most point exists, and its equal to left, then in between nodes have to be there na.
@rohandevaki4349
@rohandevaki4349 Жыл бұрын
at 14:48 , you are actually traversing the subtree to find the height right? why doesnt that count?
@raviraj-xq4ue
@raviraj-xq4ue 2 жыл бұрын
bhai aise algo sochte kaise ho yaar ??? mtlb ki gazab
@dreamyme543
@dreamyme543 Жыл бұрын
Understood:)
@sujan_kumar_mitra
@sujan_kumar_mitra 2 жыл бұрын
Understood
@ravipatel-xu5qi
@ravipatel-xu5qi 6 ай бұрын
we are traversing each node to get the heights of left or right part. So all N nodes are traversed. Even in normal pre order or post order traversal also we can count this number of nodes. Why to go for this approach.
@spicy46475
@spicy46475 Ай бұрын
becaus it has less time complexity
@subramanyah9848
@subramanyah9848 Жыл бұрын
One LIne Brute Force (Recursive) : 0(n) int countNodes(TreeNode root) { return (root == null)? 0 : 1 + countNodes(root.left) + countNodes(root.right); }
@shivangmathur6112
@shivangmathur6112 Жыл бұрын
IN cpp code if (left height == right height) we need to (1
@shashamnk2525
@shashamnk2525 Жыл бұрын
why is that needed ? 1
@shivangmathur6112
@shivangmathur6112 Жыл бұрын
@@shashamnk2525 that's how you calculate using bits 1 is represented as 00000000.......01 In binary 31 zeros then 1
@shashamnk2525
@shashamnk2525 Жыл бұрын
@@shivangmathur6112 I think we are calculating 2^h+1 because we are taking height of root as 0
@sakshisinghal1669
@sakshisinghal1669 2 жыл бұрын
Hey striver, what if the tree which you had taken example of does not have 11? In that case the left height comes out to be 3 and number of nodes we will calculate would be 7 but actually it is 6. Can you or anyone correct me if I missed anything?
@chinthaladurgapranathi8975
@chinthaladurgapranathi8975 2 жыл бұрын
TreeNode cur=root; int left=0;int right=0; while(cur!=null){ left++; cur=cur.left; } cur=root; while(cur!=null){ right++; cur=cur.right; } if(left==right)return (int)Math.pow(2,left)-1; else return countNodes(root.left)+countNodes(root.right)+1;
@sampathrakesh425
@sampathrakesh425 2 жыл бұрын
The right height would be 2.. Since we're taking the right most node for calculating the height
@kartikeysharma8904
@kartikeysharma8904 2 жыл бұрын
in that case the recursive call will function as we don't have equal leftHeight and rightHeight.
@arpitkhanulia7
@arpitkhanulia7 2 жыл бұрын
here we are calculating right height and left height , not max height of left and max height of right;
@shubhrajyotipoddar1684
@shubhrajyotipoddar1684 Жыл бұрын
explanation is kinda iffy, actually in code left height is from the left most node not left subtree( height if it were a left skew tree) and similarly for right height it calculates height from the right skew tree not the right sub-tree, thus if left skew height and right skew height is same it means no of nodes is 2^n-1 as it is complete BT. Hope you understand.
@thisismr900
@thisismr900 11 ай бұрын
can someone explain this: @9:16 without traversing any nodes below the node-2, how can directly know height and hence return 7
@sayantaniguha8519
@sayantaniguha8519 Жыл бұрын
Can we say that since this is an Almost Complete Binary Tree, which means the condition (leftHeight==rightHeight) will satisfy at-least once & not go wasted ?
@tusharvlogs6333
@tusharvlogs6333 Жыл бұрын
yeah . thats why we wont be traversing either the right or the left subtree once we find the height diference. one of the part has to satisfy this condition.
@shreyarawatvlogs6920
@shreyarawatvlogs6920 5 ай бұрын
Cant we directly write 1+ countnodes(root->left)+ countnodes(root->right). Its getting accepted in leetcode. Why to complex it and find height and all
@notNotShivam
@notNotShivam 4 ай бұрын
That will be O(n). We're supposed to do it in less than O(n)
@parasjaggi268
@parasjaggi268 2 жыл бұрын
in the ex u take ,remove 11 and find lh for 2 its 3 and rh is 3 so u would say 2^3-1=7 but as there no 11 so ans should be 6 ,rather than 7 as just with height how can u say it full
@ashitapahwa7595
@ashitapahwa7595 2 ай бұрын
i think in c++ code , it should be if(lh==rh) return (1
@sahilkhan_cs50
@sahilkhan_cs50 Жыл бұрын
📝Slightly different approach by not calculating the same nodes repeatedly for calculating the height.....We check the binary tree is faulty or not..By faulty I mean not prefect.If the tree rooted at root is faulty,then check its left and right sub trees .Any one of them will be faulty.The other will be the reverse.We can exploit this binary possibility in this way.If the left child tree is faulty,then dont recurse to the right side tree ,because the rght side tree is perfect and thus add the no of nodes acc. to the formula to the count passed as reference.If the left child tree is not faulty,i.e perfect then sum the nodes and recurse to the right child tree.This process goes on recursively .In this way,we leave many perfect subtrees and calculate the number of nodes without visiting them.And then we hit the base case where the tree is like / or . ...Then we return from there.We are not calculating repeatedly the heights again and again thus not repeteadly travelling the same nodes for finding the height...for the child trees the height will be that of parent-1 .for left child tree the left ht will be left ht of parent-1 and right ht we have to find....thus ensuring we are travelling the nodes only once ,not repeteadly.This further brings down the time.The complexity is O((logn)^2). class Solution { public: bool helper(TreeNode* root,int lh,int rh,int& count){ if(lh==rh) { count+=(1left; } helper(root->right,lhr,rh-1,count); count+=1; } return false; } int countNodes(TreeNode* root) { int lh=0,rh=0; TreeNode* node=root; while(node){ lh++; node=node->left; } node=root; while(node){ rh++; node=node->right; } int count=0; helper(root,lh,rh,count); return count; } };
@nishchaysingh804
@nishchaysingh804 2 жыл бұрын
I have a doubt that bhaiya wrote if (left != right) --> this mean that the tree is not complete but in the question it is given that the tree is complete??
@deepaksingh-qd7xm
@deepaksingh-qd7xm 3 ай бұрын
ok my question is if we are traversing the complete tree to find the height of tree why not count the nodes in same only its the same thing ???????????????????????? 🙄🙄🙄🙄🙄🙄🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔
@kotipallimadhumeher71
@kotipallimadhumeher71 2 жыл бұрын
can anyone tell what is exact meaning of the height of a tree? What i have know that the height is the longest path where no of EDGES are from a particular node to any leaf node. But according to this video,he represented height with no.of NODES not EDGES
@neerajmahapatra5239
@neerajmahapatra5239 2 жыл бұрын
Actually, you can think both the way it never matters.
@omkarlavangad1434
@omkarlavangad1434 2 жыл бұрын
It depends on what is required in the question. Generally it should be the number of levels i.e. number of nodes, however you may be asked/need to calculate number of edges in some questions
@judgebot7353
@judgebot7353 Жыл бұрын
good algo
@chhadios9959
@chhadios9959 Жыл бұрын
what if 5 has only one node the height will be the same but the formula won't work right?
@037_abhinavkumar3
@037_abhinavkumar3 10 ай бұрын
same doubt
@ASHISHKUMAR-jh9kw
@ASHISHKUMAR-jh9kw 2 жыл бұрын
In JAVA code there should be ((1
@sulthanmogal9692
@sulthanmogal9692 Жыл бұрын
also i think..it shud be 1
@ranjeet_sohanpal
@ranjeet_sohanpal 14 күн бұрын
Can somebody explain the Time Complexity point?
@thilakreddy1904
@thilakreddy1904 2 жыл бұрын
Bhai Graphs ke bad Ek video Bit manipulation ke bareme banao na...
@mriduljain1981
@mriduljain1981 Жыл бұрын
completed lecture 32 of free ka tree series.
@shashwatpriyadarshy7681
@shashwatpriyadarshy7681 2 жыл бұрын
Correction: While returning the number of nodes, in the video's solution we are returning (1
@pranavsharma7479
@pranavsharma7479 2 жыл бұрын
true
@anishgupta7553
@anishgupta7553 2 жыл бұрын
You are wrong. You didn't understood code correctly because in code , he has already included root node in height
@apurvsinha8793
@apurvsinha8793 2 жыл бұрын
or you could just start with hght=1 in both left and right height traversals then the formula will not change
@naivedhshah2980
@naivedhshah2980 2 жыл бұрын
how is it better than recurcive solution?
@rakshayadav1892
@rakshayadav1892 2 жыл бұрын
Python code: class Solution: def countNodes(self, root: Optional[TreeNode]) -> int: def rightheight(root): h=0 while root: h+=1 root=root.right return h def leftheight(root): h=0 while root: h+=1 root=root.left return h if leftheight(root)==rightheight(root): h=leftheight(root) return (2**h)-1 else: return 1+self.countNodes(root.left)+self.countNodes(root.right)
@avi7474
@avi7474 9 ай бұрын
practically, this method was .07 ms faster than the O(n) solution. Yeah nice.
@ishanporwal4403
@ishanporwal4403 Жыл бұрын
i think the logic is somewhat broken as if in the first example there is left child of 6 then then lh=rh=4 would have held true....and we would have returned 2^4-1=15 but the ans is 12
@gigachad2419
@gigachad2419 Жыл бұрын
Striver is Literally God for DSA.
@siddarthreddykoppera5930
@siddarthreddykoppera5930 2 жыл бұрын
I have got a doubt, that is If there is a node connected to 6 then we get LeftHeight==RightHeight==3 and the answer will be (2^3-1=7): 1+7+7=15 which is wrong. Did I do any mistake or forgot to consider something? Please Help!!
@Rohitkumar-me1uc
@Rohitkumar-me1uc 2 жыл бұрын
same doubt
@AmanSingh-on4ce
@AmanSingh-on4ce 2 жыл бұрын
how? left height is 3 and right height is 2 even when u add to node(6)
@herculean6748
@herculean6748 Жыл бұрын
if 6 nodes connected, then left height(extreme left) will be 3 and right height(extreme right) will be 2 lh != rh then 1 + countNodes(root->left) + countNodes(root->right) 1+3+2 (it will return)
@dipeshsaili4468
@dipeshsaili4468 2 жыл бұрын
on leetcode, with O(N) time is 50 ms but using O(log^2N) it's 75 ms :C
@yadneshkhode3091
@yadneshkhode3091 2 жыл бұрын
the time on leetcode is based on anything but time complexity
@virusdgamer8804
@virusdgamer8804 Жыл бұрын
what if the leaf right is not there on a right subtree parent but the parent left subtree is full? In the given example...if node with value 11 is not there...node 2 will give both left and right heights to be 3, but since there is no 11, the number of nodes will not be 2^height - 1 Edit: I saw the technique that he used to find the height and that solves the above problem. This is not the traditional way of finding height of the tree, otherwise it would have killed the point of improving the time complexity anyways.
@raunak1833
@raunak1833 Жыл бұрын
thanks
@letsrock7354
@letsrock7354 Жыл бұрын
At 5:54 what if 9 and 10 were not there then as per our code it would give equal height..right? correct me if I m wrong
@someshpatel7660
@someshpatel7660 4 күн бұрын
Yes exactly. Thats what I was wondering about. Complete BT might not have right sibling but height will still be same in that case, this formula might be wrong?
@tusharnain6652
@tusharnain6652 2 жыл бұрын
In strivers code, when we passes root to getHeight function, isnt that gonna change root to NULL. Take a look at that please!
@princiagrawal1428
@princiagrawal1428 Жыл бұрын
I guess not because we are not passing the root by reference
@tusharnain6652
@tusharnain6652 Жыл бұрын
@@princiagrawal1428 my bad
@shashamnk2525
@shashamnk2525 Жыл бұрын
Do we know the explanation of Bitwise leftshift operation is done on 1 in CPP and 2 on JAVA ? I am not sure why we are using 2
@shashamnk2525
@shashamnk2525 Жыл бұрын
So i did some digging if the height of root is considered 0 then formula for total number of nodes is 2^(h+1) -1, if the height of the root is considered 1 then the formula is 2^h -1.
@mdrehankhan3458
@mdrehankhan3458 Жыл бұрын
it will be 1
@shashamnk2525
@shashamnk2525 Жыл бұрын
@@mdrehankhan3458 yes if we are counting height from zero then yes
@ishaankaustav727
@ishaankaustav727 2 жыл бұрын
💚💚💚
L33. Requirements needed to construct a Unique Binary Tree | Theory
8:41
L31. Minimum time taken to BURN the Binary Tree from a Node | C++ | Java
18:24
Inside Out Babies (Inside Out Animation)
00:21
FASH
Рет қаралды 23 МЛН
Викторина от МАМЫ 🆘 | WICSUR #shorts
00:58
Бискас
Рет қаралды 5 МЛН
Big-O Notation - For Coding Interviews
20:38
NeetCode
Рет қаралды 440 М.
L12. Largest Rectangle in Histogram | Stack and Queue Playlist
31:42
Count Complete Tree Nodes | Leetcode #222
13:39
Techdose
Рет қаралды 32 М.
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 288 М.
L21. Reverse Nodes in K Group Size of LinkedList
24:31
take U forward
Рет қаралды 69 М.
Why Do Bubbles Form In Glasses Of Water?
12:33
Joe Scott
Рет қаралды 63 М.
G-27. Shortest Path in Directed Acyclic Graph - Topological Sort
26:36
take U forward
Рет қаралды 199 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 633 М.