Count Complete Tree Nodes | Leetcode

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ม.ค. 2025

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

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

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

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

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

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

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

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

    @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 < d; ++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 3 ปีที่แล้ว

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

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

    So much of concepts in a single video🙏🏼

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

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

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

      Thanks :)

    • @PIYUSH-lz1zq
      @PIYUSH-lz1zq 3 ปีที่แล้ว

      @@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 3 ปีที่แล้ว

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

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

    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

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

    I love your explanations.

  • @VarunKaushal-zx9zq
    @VarunKaushal-zx9zq 3 หลายเดือนก่อน +1

    Fantastic Explanation

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

      thanks

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

    Thank you, you explain it clearly.💯

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

    Very nice explanation sir. God bless you.

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

      Thanks

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

    Superb. Great work sir

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

    Excellent explanation!

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

    Thanks, it was really helpfull explanation!

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

    Amazing explanation, thank you for your help!

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

    explanation is amazing.

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

    Sooo good!!

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

    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?

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

    Genius. Very impressive

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

    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.

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

    amazing explanations !!!!

  • @ShreyaSingh-vr9qi
    @ShreyaSingh-vr9qi 4 ปีที่แล้ว +1

    Nice explaination !!

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

    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 ปีที่แล้ว +1

      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

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

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

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

      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.

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

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

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

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

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

    Thanks Sir 👍

  • @mayur.a15
    @mayur.a15 2 ปีที่แล้ว

    what is N in time complexity?

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

    great explaination

  • @rashmikiranpandit8962
    @rashmikiranpandit8962 4 ปีที่แล้ว

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

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

    better explanation

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

    @ 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 3 ปีที่แล้ว +1

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

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

    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 ปีที่แล้ว +2

      Wacom pro inkspace sketch io

    • @rohiniloya2964
      @rohiniloya2964 4 ปีที่แล้ว

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

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

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

  • @adityakumar-bg9qn
    @adityakumar-bg9qn 3 ปีที่แล้ว +1

    Great

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

    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 ปีที่แล้ว +2

      my bad q's says its complete binary tree.

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

    Sir how to calculate total depth and what is total depth

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

    Thank you

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

    thank you!

  • @Jake-fn2iy
    @Jake-fn2iy 3 ปีที่แล้ว +1

    Nice

  • @PrashantSingh-pg9vq
    @PrashantSingh-pg9vq 3 ปีที่แล้ว

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

  • @md_aaqil8027
    @md_aaqil8027 4 ปีที่แล้ว

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

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

    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 :)

  • @shikharchaudhary6984
    @shikharchaudhary6984 4 ปีที่แล้ว

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

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

    👍🏻