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

  • @jyotirmoydey9907
    @jyotirmoydey99073 жыл бұрын

    why? why? why? why did I not watch this earlier... an amazing explanation as always. Thank you very much!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @vinayak186f3

    @vinayak186f3

    3 жыл бұрын

    Were you got asked this question in an interview ?

  • @ShubhamKumar-of1vq

    @ShubhamKumar-of1vq

    2 жыл бұрын

    Overacting

  • @anitasrivastava983
    @anitasrivastava9834 жыл бұрын

    The man , the myth , the legend uploads once again!

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    😅

  • @spetsnaz_2
    @spetsnaz_24 жыл бұрын

    So much of concepts in a single video🙏🏼

  • @mehulsolanki9435
    @mehulsolanki9435 Жыл бұрын

    Your explanations are very clear. Clean and concise! Thanks.

  • @frostcs
    @frostcs4 жыл бұрын

    @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

    @techdose4u

    4 жыл бұрын

    Yea, we can use loop to avoid space.

  • @sanjeevashoka7948

    @sanjeevashoka7948

    4 жыл бұрын

    what actually happens to pointer after deleteing. code:- int main() { int a=5; int *p=&a; cout

  • @anonymous-sk2pr

    @anonymous-sk2pr

    2 жыл бұрын

    @@sanjeevashoka7948 delete operation is invalid on static memory it only is valid on heap memory

  • @kunalsoni7681
    @kunalsoni76814 жыл бұрын

    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

  • @peterhessey7732
    @peterhessey7732 Жыл бұрын

    Amazing explanation, thank you for your help!

  • @adilmolzhigitov4381
    @adilmolzhigitov4381Ай бұрын

    Thanks, it was really helpfull explanation!

  • @chibata_one730
    @chibata_one7305 ай бұрын

    Thank you, you explain it clearly.💯

  • @agileprogramming7463
    @agileprogramming74634 жыл бұрын

    Sooo good!!

  • @raj_kundalia
    @raj_kundalia11 ай бұрын

    thank you!

  • @thebetamalespodcast7182
    @thebetamalespodcast7182 Жыл бұрын

    amazing explanations !!!!

  • @RoySmith264
    @RoySmith2642 жыл бұрын

    Genius. Very impressive

  • @goutamkundu6392
    @goutamkundu63923 жыл бұрын

    Loved your explanation. The code is very simple. But you explained the dry run very nicely.

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks :)

  • @PIYUSH-lz1zq

    @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

    @PIYUSH-lz1zq

    2 жыл бұрын

    @@techdose4u can you tell me why this logic is wrong

  • @mondayemmanuel191
    @mondayemmanuel1913 жыл бұрын

    I love your explanations.

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    😊

  • @_aka5h
    @_aka5h Жыл бұрын

    Thank you

  • @rashmikiranpandit8962
    @rashmikiranpandit89624 жыл бұрын

    Thanks for nice explanation Can you please explain again how did the time complexity came out like that

  • @arindam1249
    @arindam1249 Жыл бұрын

    better explanation

  • @aayushvrshney
    @aayushvrshney4 жыл бұрын

    Excellent explanation!

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks

  • @rashmikiranpandit8962
    @rashmikiranpandit89624 жыл бұрын

    Interested to know which software do u use to explain and write on? Is it free?

  • @thunderbolt8632
    @thunderbolt86324 жыл бұрын

    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

    @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

  • @rehanakhter4813
    @rehanakhter48133 жыл бұрын

    Superb. Great work sir

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks :)

  • @adityakumar-bg9qn
    @adityakumar-bg9qn3 жыл бұрын

    Great

  • @royarijit998
    @royarijit9984 жыл бұрын

    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

    @techdose4u

    4 жыл бұрын

    Welcome :)

  • @lifecodesher5818

    @lifecodesher5818

    3 жыл бұрын

    what solution would work in interview?

  • @ShreyaSingh-vr9qi
    @ShreyaSingh-vr9qi4 жыл бұрын

    Nice explaination !!

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks

  • @rohiniloya2964
    @rohiniloya29644 жыл бұрын

    What writing app do you use for writing? I like the color scheme of this app. Much appreciated if you could share

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Wacom pro inkspace sketch io

  • @rohiniloya2964

    @rohiniloya2964

    4 жыл бұрын

    TECH DOSE could not find it online . Any link will help

  • @MrThepratik
    @MrThepratik4 жыл бұрын

    great explaination

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks :)

  • @bishwajeetsharma3498
    @bishwajeetsharma34983 жыл бұрын

    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?

  • @ankitkumarsingh9815
    @ankitkumarsingh98154 жыл бұрын

    Thanks Sir 👍

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Welcome :)

  • @asahikitase5398
    @asahikitase53982 жыл бұрын

    explanation is amazing.

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Thanks 😊

  • @VinothiniAnabayan
    @VinothiniAnabayan3 жыл бұрын

    hi can you please make videos on low level system design. superb explanation..

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    I am currently teaching system design in my classes. You can join that if you wanna learn full system design

  • @mayuraitavadekar7968
    @mayuraitavadekar7968 Жыл бұрын

    what is N in time complexity?

  • @sayantaniguha8519
    @sayantaniguha8519 Жыл бұрын

    Can you explain the time complexity for a skew-tree ?

  • @nithish_raina

    @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.

  • @rahulagarwal8059
    @rahulagarwal80594 жыл бұрын

    👍🏻

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    👍

  • @ijaz2020
    @ijaz20204 жыл бұрын

    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

    @ijaz2020

    4 жыл бұрын

    my bad q's says its complete binary tree.

  • @junaidiqbal2321
    @junaidiqbal23213 жыл бұрын

    Sir how to calculate total depth and what is total depth

  • @Jake-fn2iy
    @Jake-fn2iy3 жыл бұрын

    Nice

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks

  • @rahulshirole7576
    @rahulshirole75762 жыл бұрын

    @ 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

    @mdazharuddin4684

    2 жыл бұрын

    Question is about Complete Binary Tree whose property doesn't allow both sided skewed trees.

  • @yitingg7942
    @yitingg79423 жыл бұрын

    Hi sir, Log N * Log N will be faster than N ? Sorry lol I totally forgot all them math..

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Take a large value of N and calculate. You will get it :)

  • @PrashantSingh-pg9vq
    @PrashantSingh-pg9vq3 жыл бұрын

    skipped this question many time.... but after seeing this... i am like....whyyy???

  • @shikharchaudhary6984
    @shikharchaudhary69843 жыл бұрын

    Technically, It's not depth, it's height . And Max depth = height.

  • @md_aaqil8027
    @md_aaqil80274 жыл бұрын

    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); } }