Maximum sum of Non-adjacent nodes in Binary Tree GeeksforGeeks Problem of the day C++ Code & Example

Поделиться
HTML-код
  • Опубликовано: 9 сен 2024

Комментарии • 47

  • @probabilitycodingisfunis1
    @probabilitycodingisfunis1  2 года назад +2

    Time Complexity & Space Complexity: O(N)
    Code:
    unordered_mapm;
    int getMaxSum(Node *root)
    {
    // Add your code here
    //recursive code
    //optimize -> 2 lines
    //with node
    //case 1
    if(!root)return 0;
    if(m[root])return m[root];
    int withnode = root->data;
    //call your grandchildren
    if(root->left)
    { withnode+=getMaxSum(root->left->left);
    withnode+=getMaxSum(root->left->right);
    }
    if(root->right)
    {
    withnode+=getMaxSum(root->right->left);
    withnode+=getMaxSum(root->right->right);
    }
    //case 2
    //without node
    //call your children
    int withoutnode = getMaxSum(root->left)+getMaxSum(root->right);
    return m[root]= max(withnode,withoutnode);
    }

  • @anuragkhugshal4994
    @anuragkhugshal4994 2 года назад +4

    Good and intuitive approach, other channels are mostly using the same textbook approach.

  • @letscodewithshivam
    @letscodewithshivam Год назад +3

    Very good explanation, recursion works like magic. Thanks

  • @felixrajkumar2796
    @felixrajkumar2796 2 года назад +3

    Thank god, she explained in English ! Also her intuition to get into the solution is pretty good.

  • @silajmunna2854
    @silajmunna2854 2 года назад +2

    At start I haven't even understood the problem but the way you explained the solution is wow...... Superb ❤️

  • @vignanreddy3252
    @vignanreddy3252 2 года назад +1

    Damn! Can’t expect for more clear explanation.

  • @TarangRastogi-l4x
    @TarangRastogi-l4x Месяц назад

    Very Nice explanation madam . Keep working hard

  • @suryansh70
    @suryansh70 2 года назад +2

    this is the best explanation ever possible very well explained very easy to understand thank u

  • @debabratamukherjee9577
    @debabratamukherjee9577 Месяц назад

    Excellent Explanation.

  • @ratishjain2718
    @ratishjain2718 Год назад

    The way you explain intuition is brilliant !! Nice work

  • @anshul7628
    @anshul7628 2 года назад +1

    Great explanation .🙂👍

  • @vinamrasangal8436
    @vinamrasangal8436 Год назад

    you are just amazing alisha

  • @farazjawaid2982
    @farazjawaid2982 Год назад

    how the fuck can someone explain this so beautifully
    great stuff maam !!

  • @sarveshkanwar1300
    @sarveshkanwar1300 11 месяцев назад

    mann gayee awesome explanation

  • @adityavaste8963
    @adityavaste8963 Год назад

    Wonderful Explanation

  • @anandverma4357
    @anandverma4357 2 года назад

    Good approach ma'am
    Very good approach

  • @aqua6150
    @aqua6150 2 года назад

    Underrated Channel

  • @gowthamshankark6779
    @gowthamshankark6779 2 года назад

    excellent and so easy to understand

  • @dasarisaikiran7206
    @dasarisaikiran7206 2 года назад +1

    nice explanation ma'am

  • @sidddddddoo7
    @sidddddddoo7 2 года назад

    This is so good!
    Thank you!

  • @abhigyanraha5620
    @abhigyanraha5620 2 года назад

    thanks for the great explanation!

  • @AmitKumar-hr3pc
    @AmitKumar-hr3pc 2 года назад

    very good problem solving approach

  • @sharadyadav9630
    @sharadyadav9630 2 года назад

    Good explain

  • @felixrajkumar2796
    @felixrajkumar2796 2 года назад

    Keep on the good work !

  • @rishupal7904
    @rishupal7904 2 года назад

    Damn! nice explanation

  • @user-gc5pr8gp9c
    @user-gc5pr8gp9c 10 месяцев назад

    Wow

  • @showsshorts4696
    @showsshorts4696 Год назад

    mam after how many months pf practice you solved this approach

  • @VishalSingh-oj7go
    @VishalSingh-oj7go 2 года назад

    this was good !

  • @midhileshmomidi3120
    @midhileshmomidi3120 2 года назад

    I did like level order traversal and did 1d dp kind but I got wrong ans for this test case [2,1,3,null,4]
    ans is given 7 but isn't it 6
    tree : 2
    1 3
    4

    • @probabilitycodingisfunis1
      @probabilitycodingisfunis1  2 года назад

      If 4 is a child of 1 and not 3 , then 4 and 3 can be taken together and the answer will be 7

    • @midhileshmomidi3120
      @midhileshmomidi3120 2 года назад

      @@probabilitycodingisfunis1 Thank you for explanation. I got it I now. I misunderstood the question, thought complete level should be skipped

  • @malkeetsingh3147
    @malkeetsingh3147 2 года назад

    Awesome Explaination

  • @loveleshbhagat1114
    @loveleshbhagat1114 2 года назад

    unordered_mapm;
    int getMaxSum(Node *root)
    {
    //Recursive Solution ->Optimization using unordered_map
    //Base Case
    if(root==NULL) return 0;
    if(root->left==NULL && root->right==NULL) return root->data;
    if(m.find(root)!=m.end()) return m[root];
    // Case 1
    int withnode = root->data;
    if(root->left!=NULL){
    withnode += getMaxSum(root->left->left);
    withnode += getMaxSum(root->left->right);
    }
    if(root->right!=NULL){
    withnode += getMaxSum(root->right->left);
    withnode += getMaxSum(root->right->right);
    }
    //Case 2:
    int withoutnode = getMaxSum(root->left)+getMaxSum(root->right);
    m[root] = max(withnode,withoutnode);
    return max(withnode,withoutnode);
    }

  • @gorakhkumargupta491
    @gorakhkumargupta491 2 года назад

    nice explanation ma'am