Count Complete Tree Nodes | Leetcode
This video explains a very important programming interview problem which is to count the number of nodes in a given complete binary tree.This problem seems to be very simple if we are allowed to solve in O(N) linear time by using simple recursion, inorder, preorder, postorder traversal techniques.But can we improve the time complexity to logarithmic time? In this video i have shown how we can improve the time complexity by using the property of complete binary tree.The time complexity of the efficient approach is O(logN * logN).I have explained the algorithm using proper examples and code is explained at the end of the video. CODE LINK is present below as usual. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
=================================================================
INSTAGRAM: / surya.pratap.k
LinkedIn: / surya-pratap-kahar-47b...
WEBSITE: techdose.co.in/
=================================================================
CODE LINK: gist.github.com/SuryaPratapK/...
OTHER PROBLEMs:-
Search in a Binary Search Tree: • Search in a Binary Sea...
Invert Binary Tree: • Invert Binary Tree | L...
Cousins in a binary tree: • Cousins in a binary tr...
Пікірлер: 73
why? why? why? why did I not watch this earlier... an amazing explanation as always. Thank you very much!
@techdose4u
3 жыл бұрын
Welcome :)
@vinayak186f3
3 жыл бұрын
Were you got asked this question in an interview ?
@ShubhamKumar-of1vq
2 жыл бұрын
Overacting
The man , the myth , the legend uploads once again!
@techdose4u
4 жыл бұрын
😅
So much of concepts in a single video🙏🏼
Your explanations are very clear. Clean and concise! Thanks.
@TechDose There can be more optimization done for space complexity as here we are using recursion so the stack size will be Theta(N) Here is the sample implementation: ``` class Solution { // Return tree depth in O(d) time. public int computeDepth(TreeNode node) { int d = 0; while (node.left != null) { node = node.left; ++d; } return d; } // Last level nodes are enumerated from 0 to 2**d - 1 (left -> right). // Return True if last level node idx exists. // Binary search with O(d) complexity. public boolean exists(int idx, int d, TreeNode node) { int left = 0, right = (int)Math.pow(2, d) - 1; int pivot; for(int i = 0; i pivot = left + (right - left) / 2; if (idx right). // Perform binary search to check how many nodes exist. int left = 1, right = (int)Math.pow(2, d) - 1; int pivot; while (left
@techdose4u
4 жыл бұрын
Yea, we can use loop to avoid space.
@sanjeevashoka7948
4 жыл бұрын
what actually happens to pointer after deleteing. code:- int main() { int a=5; int *p=&a; cout
@anonymous-sk2pr
2 жыл бұрын
@@sanjeevashoka7948 delete operation is invalid on static memory it only is valid on heap memory
This video is a Very helpful to understand problem of complete binary tree.. I'm saying thanking you sir most 😊 .. to make a such beautiful explanation
Amazing explanation, thank you for your help!
Thanks, it was really helpfull explanation!
Thank you, you explain it clearly.💯
Sooo good!!
thank you!
amazing explanations !!!!
Genius. Very impressive
Loved your explanation. The code is very simple. But you explained the dry run very nicely.
@techdose4u
3 жыл бұрын
Thanks :)
@PIYUSH-lz1zq
2 жыл бұрын
@@techdose4u int countNodes(TreeNode* root) { if(!root) return 0; int res=0; if(root->left && root->right) res++; res +=countNodes(root->left) + countNodes(root->right); return res; }
@PIYUSH-lz1zq
2 жыл бұрын
@@techdose4u can you tell me why this logic is wrong
I love your explanations.
@techdose4u
3 жыл бұрын
😊
Thank you
Thanks for nice explanation Can you please explain again how did the time complexity came out like that
better explanation
Excellent explanation!
@techdose4u
4 жыл бұрын
Thanks
Interested to know which software do u use to explain and write on? Is it free?
Hello Sir,I have a doubt regarding one problem,I'm not sure if that can be done by DP. The question is similar to minimum cost path sum but instead of going from (0,0) to(n,n),we can go from any point of first column to any point of last column ((x,0)->(y,n))!Allowed moves are:up,down,right can u please suggest how can I approach this problem?
@codelogic8918
2 жыл бұрын
its basically has a variable starting and ending points...so u can just make loop for every starting element of 0th row and return max
Superb. Great work sir
@techdose4u
3 жыл бұрын
Thanks :)
Great
Great explanation, Sir!! However, I was surprised to see the trivial dfs based solution not giving TLE upon submission. It may have worked on Leetcode, the dfs solution would definitely not work in an actual interview (we are required to do better). Thanks for explaining the more intuitive approach.
@techdose4u
4 жыл бұрын
Welcome :)
@lifecodesher5818
3 жыл бұрын
what solution would work in interview?
Nice explaination !!
@techdose4u
4 жыл бұрын
Thanks
What writing app do you use for writing? I like the color scheme of this app. Much appreciated if you could share
@techdose4u
4 жыл бұрын
Wacom pro inkspace sketch io
@rohiniloya2964
4 жыл бұрын
TECH DOSE could not find it online . Any link will help
great explaination
@techdose4u
4 жыл бұрын
Thanks :)
So basically what we are doing is T(h) = O(h) + T(h-1) where h is the height of the tree. Therefore time complexity is O(h^2) and since h= log N in case of complete binary tree therefore time complexity is logN *logN? Am i correct?
Thanks Sir 👍
@techdose4u
4 жыл бұрын
Welcome :)
explanation is amazing.
@techdose4u
2 жыл бұрын
Thanks 😊
hi can you please make videos on low level system design. superb explanation..
@techdose4u
3 жыл бұрын
I am currently teaching system design in my classes. You can join that if you wanna learn full system design
what is N in time complexity?
Can you explain the time complexity for a skew-tree ?
@nithish_raina
Жыл бұрын
Skewed tree cannot be a complete binary tree hence counting no of nodes of a skewed tree is possible by applying any traversal techinque with O(N) TC.
👍🏻
@techdose4u
4 жыл бұрын
👍
this is nice video. I have a clarification. what if tree like this 0 0 0 0 0 our program return 7 but node itself is 5.
@ijaz2020
4 жыл бұрын
my bad q's says its complete binary tree.
Sir how to calculate total depth and what is total depth
Nice
@techdose4u
3 жыл бұрын
Thanks
@ 4:21 u said left level=right level=4 hence perfect bst ..how?? it can be skewed from both ends but still not perfect bst right??
@mdazharuddin4684
2 жыл бұрын
Question is about Complete Binary Tree whose property doesn't allow both sided skewed trees.
Hi sir, Log N * Log N will be faster than N ? Sorry lol I totally forgot all them math..
@techdose4u
3 жыл бұрын
Take a large value of N and calculate. You will get it :)
skipped this question many time.... but after seeing this... i am like....whyyy???
Technically, It's not depth, it's height . And Max depth = height.
class Solution { public int countNodes(TreeNode root) { if(root==null) return 0; int lc=1; TreeNode lt=root.left; while(lt!=null) { lt=lt.left; lc++; } int rc=1; TreeNode rt=root.right; while(rt!=null) { rt=rt.right; rc++; } if(lc==rc) return (int)Math.pow(2,lc)-1; return 1+countNodes(root.left)+countNodes(root.right); } }