Max Sum Path from One Node to Another in Binary Tree | C++ Placement Course | Lecture 27.16

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 ก.ย. 2024
  • Complete C++ Placement Course (Data Structures+Algorithm) : • C++ Full Course | C++...
    Telegram: t.me/apnikaksh...
    Instagram: / dhattarwalaman
    Notes of this Lecture:

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

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

    great explanation mam, the dry run of the code really helps a lot!!

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

    Really doing great job falling in love with the channel ❤️❤️❤️👍

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

    I am in serious need of explanations of recursion like this 🥺it was best.

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

      why are we considering max path through left + maxpath through right + node val in case 4 .............in case 3 hmne to condition le li thi na left child + node val ki
      phir 4th case me vhi condition q??

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

      @@sdexstarlord Suppose u have a BT as root=5, left of root=7, right of root=9.
      So here, val=5 @root
      Left+val=12
      Right+val=14
      Left+val+Right=21
      And max of all these = 21
      So, 21 will be our answer

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

      @@doodle7703 but in code she is returning max(root->data, max(left, right)+root->data) then how can 21 be answer if it is root-data + right+ left

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

    for the whole tree we consider the maximum of all 4 (rootdata,rootdata+left,rootdata+right,rootdata+left+right) and for the root's subtrees we try to find a maximum sum path by considering max of (nodedata,nodedata+left,nodedata+right),from these subtrees left and right are given for calculation to the node tree.

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

    Beautiful Explanation Mam❤️, Respect ++

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

    where the value of singlepathsum goes after returning from func (backtrack) does it go on left or right variable or where??

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

    I am not a college student but i appreciate your effort aman bhaiya and team

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

    Hats off for the explanation...... after explanation it was very easy to write the code.

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

    aman bhaiya thanks (hats off)

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

    int maxPathsum(node* root){
    if(root==NULL){
    return 0;
    }
    int ans = INT_MIN;
    int a=root->data;
    int b=maxPathsum(root->left)+root->data;
    int c=maxPathsum(root->right)+root->data;
    int d=maxPathsum(root->left)+maxPathsum(root->right)+root->data;
    int temp=max(max(a, b), max(c, d));
    ans=max(ans, temp);
    return ans;
    }
    This works as well...

  • @Sr-uv4fd
    @Sr-uv4fd 3 ปีที่แล้ว +6

    Can someone explain me why is the second value (node value + right + left) I mean I understand about the other 3 values but what exactly is the logic behind ( node value + right + left)

  • @PrashantSingh-d97
    @PrashantSingh-d97 3 ปีที่แล้ว +5

    Always waiting for the new lesson

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

    Gajab❤️

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

    Aman bhaiya please make video on startup fund raising
    Recently you talked in tedx video

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

    then what is use of ans in masutil function

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

      I also have doubt in this part Why she calculated ans when there is no need of it

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

    In maxPathUtil function pass ans as a ( & ) reference insted of pass by value
    : ) Happy coding

  • @Jayanth-xn2nv
    @Jayanth-xn2nv 2 ปีที่แล้ว +3

    if we put +6 in place of -6 in the example tree
    then the ans will be 15 according to the program
    but the ans should be 10
    i think the problem is because we include nodeval + leftmax + right max

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

    #include
    using namespace std;
    struct Node {
    int data;
    struct Node*left;
    struct Node*right;
    Node(int val){
    data=val;
    left =NULL;
    right=NULL;
    }
    };
    int maxPathsum(Node* root){
    if(root==NULL){
    return 0;
    }
    int ans = INT_MIN;

    int a=root->data;
    int b=maxPathsum(root->left)+root->data;
    int c=maxPathsum(root->right)+root->data;
    int d=maxPathsum(root->left)+maxPathsum(root->right)+root->data;

    int temp=max(max(a, b), max(c, d));
    ans=max(ans, temp);
    return ans;
    }
    int main()
    {

    struct Node* root = new Node(1);
    root->left = new Node(-2);
    root->right = new Node(4);
    root->left->left = new Node(4);
    root->right->right = new Node(-5);
    cout

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

    This Man Is Going Crazy Keep It Up 😆 Thanks You So Much For All The Lectures❤️🙏

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

    Sister is there any efficient way of inserting elements into binary tree ,if there plz provide the code for inserting elements into binary tree dynamically

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

      There is a way... In these videos there is a VIdeo on Build a Binary Tree from Preorder. You have to give array of elements which you want to insert in Preorder and Inorder. And then just call the function buildtree and it will build a tree for you. Hope this clears your doubt.

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

    if feels like just finishing the topic anyway .

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

    Bhot hard😥🔥🔥

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

    Thank you 🙏🙏

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

    really nice video

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

    In maxPathSumUtil why we calculated ans if we have to return the value of singlePathSum????

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

      I don't get why we have calculated single path sum

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

      Same doubt and in ans there is whole path traversal but in single path there is no whole path traversal

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

      Ans is calculated for whole path traversal and no need to return ans because it is stored in address of variable so it gets changed automatically

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

      cause it only updates the ans and the function made to return values only at a specific node's right or left ,right and left of a node may be included but its not necessary that it is included

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

      Yaa
      Even if we do that to modify and,it won't as it is not send as pointers but the function take the values not variables in consideration.

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

    why ans variable is used if return is of singlepathsum variable?????

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

      exactly

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

      That is beacuse we are calculating maxpathsum between any two nodes of the tree and not just the root and leaf node.

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

    Waiting for android studio

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

      Me also

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

      Aman bhaiya please make video on startup fund raising
      Recently you talked in tedx video

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

    Initially music is good , but when teaching starts it becomes irritating . I advise you to turn it off .

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

      true, it distracts us understanding the starting explanation. Have to watch it 2-3 times in beginning to understand

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

    Maximum path mein Root to leaf node tak ki calculation must hai, I mean to say is if here root 1 ki jagah -1 hota toh value 8 hoti ya phr 7?

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

    #include
    #include
    using namespace std;
    class Node{
    public:
    int data;
    Node* left;
    Node* right;
    Node(int val){
    data=val;
    left=NULL;
    right=NULL;
    }
    };
    int maxPathSumUntil(Node* root, int &ans){
    if(root==NULL){
    return 0;
    }
    int left=maxPathSumUntil(root->left, ans);
    int right=maxPathSumUntil(root->right, ans);
    int nodeMax=max(max(root->data, root->data+left+right), max(root->data+left, root->data+right));
    ans=max(ans, nodeMax);
    int singlePsum=max(root->data, max(root->data+left, root->data+right));
    return singlePsum;
    }
    int maxPathSum(Node* root){
    int ans=INT_MIN;
    maxPathSumUntil(root, ans);
    return ans;
    }
    int main(){
    Node* root=new Node(1);
    root->left=new Node(2);
    root->right=new Node(3);
    root->left->left=new Node(4);
    root->right->right=new Node(5);
    cout

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

    very poor sound quality!!

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

    Kya koi senior bata sakta hei ye c++ ka course kaisa hei?
    Kya mujhe is channel se seekhna chaiye ya kisi aur channel se??

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

      course to achha hai pr ye kab tak khatam hoga ye nahi pata .
      maine coding isi course se start ki thi uske baad me gfg aur coding ninja ka course se kr raha hu

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

      @@vikasmane8851 bhai coding ninjas ke course ke baare mai bata sakte ho ki like kesa structure hai unka

    • @Ajeet-Yadav-IIITD
      @Ajeet-Yadav-IIITD 3 ปีที่แล้ว +3

      Yess, you can follow this course without any single doubt, I'm also revising my DSA concepts by this playlist.

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

      @@pranavverma5291 concept explain karte hai aur questions karate hai
      like ye apna college ke jaisa hi hai course but coding ninja wale thoda jyada explain karte hai aur logic ko boht jyada explain karte hai

    • @MANPREETSINGH-ft9hd
      @MANPREETSINGH-ft9hd 3 ปีที่แล้ว

      @@vikasmane8851 Bro coding ninja ke course ka price kitna hai bro please reply

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

    Is there any difference between Node* root and Node *root?

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

      no

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

      nope

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

      Na

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

      no, but the correct syntax is Node *root

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

      Basically you rae making pointer of type Node. So if you are making multiple then you need to add * in front of all pointer variable. Eg: Node *root1, *root2;
      Eg: Node* root1, *root2;
      Both above examples are correct;
      But Node* root1, root2; is not correct.

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

    please share notes 🙏🙏🙏

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

    // code in video
    #include
    #include
    using namespace std;
    struct Node{
    int data;
    Node *left, *right;
    Node(int val){
    data = val;
    left = NULL;
    right = NULL;
    }
    };
    int maxPathAumUtil(Node *root, int &ans){
    if(root == NULL){
    return 0;
    }
    int left = maxPathAumUtil(root -> left, ans);
    int right= maxPathAumUtil(root -> right, ans);
    int nodeMax = max(max(root->data, root->data + left + right),
    max(root->data+ left, root->data + right));
    ans = max(ans, nodeMax);
    int singlePathsum = max(root->data, max(root->data + left, root -> data + right));
    return singlePathsum;
    }
    int maxPathSum(Node *root){
    int ans = INT_MIN;
    maxPathAumUtil(root, ans);
    return ans;
    }
    int main()
    {
    Node *root = new Node(1);
    root->left = new Node(2);
    root->right = new Node(3);
    root->left->left = new Node(4);
    root->right->left = new Node(5);
    // test
    cout

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

    Please notes public krdo 🥲
    Bhaut dikkat ho rhi hai without practice questions 😢

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

    plz help!
    Que samjh nhi ayya
    Max distance from root nikalna h ya max distance left + max dis right nikalna from root nikalna h?

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

      Max possible sum (sum of value at node) nikalna hai whether it is of just left subtree or right subtree or both including ancestor.

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

    Code is right but initial explanation was wrong

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

    💖💖💖💖💖💖💖💖💖💖

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

    If we do +12 instead of -12 then, why it is giving 25 instead of 17 🧐🧐

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

      Coz the path in that case will be
      4-> 12 -> 1 -> 3 -> 5
      ,which adds to give 25

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

      @@shikhamaurya4453 thanks got it 👍👍

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

    Can you first try to explain the intuition behind the approach instead of jumping directly to the approach?

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

    Didi samajh nahi aaya bahut comolex ho gaya hai yeh problem ab tak kisimein bhi itni takleef nahi huu

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

    Yaar koi ye meko samajyega .......apan ye single path sum kyu nikal rahe

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

    Why am I geting lowest possible value?

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

    Can anyone explain me why we have calculate single path sum because we are not storing its value in any variable .

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

      we r returning single path because either left of the node or right of the node will be part of the max sum path not both if both r returned then path wil have branches which we dont want

    • @ARYANSHARMA-ph8ss
      @ARYANSHARMA-ph8ss 3 ปีที่แล้ว

      @@learningoverflow3138 So why are we taking that possibility in the first place? Why do we not take something like
      ans = max(root->data+left, root->data+right);
      ans = max(ans, root->data);

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

      @@ARYANSHARMA-ph8ss no ans is max of all the four quantities left,right,left+right,node->data
      u might be confused between the util function and the actual answer
      the ans is sum of whole path right
      but the util function just decides what branch of the tree should be part of the path

    • @ARYANSHARMA-ph8ss
      @ARYANSHARMA-ph8ss 3 ปีที่แล้ว

      @@learningoverflow3138 Yes and I am not able to understand how can left+right both can be the branch path. If you can explain me that would be awesome :)

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

      @@ARYANSHARMA-ph8ss yea it cannot be unless its the topmost node
      but while checking the answer in util function we have to check whether the node we r currently at has sum(that is basically checking should the node be added to the current path or should it be the topmost node) greater then the previous node
      hope u have understood it !
      ask if u have doubt

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

    i dont know why but this is giving lowest integer value

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

      store the answer returned by utility function in a variable, and then print max of ans and that variable

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

      @@anujmaurya2786 thanks just tried it's working now

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

    Please someone explain why this code is not working for negative numbers?
    int maxPathsum(Node* root){
    if(root==NULL) return 0;
    int val = INT_MIN;
    val = root->data;
    int leftSubtree = maxPathsum(root->left);
    int rightSubtree = maxPathsum(root->right);
    val +=max(leftSubtree,rightSubtree);
    return val;
    }