Print Root to Leaf Path with Given sum(Print all K-Sum paths) in a given Binary Tree

Поділитися
Вставка
  • Опубліковано 19 вер 2024

КОМЕНТАРІ • 48

  • @agarwalminal
    @agarwalminal 7 років тому +25

    Its pre-order traversal as the root node is being processed first.

  • @Rohankumar-wh3uu
    @Rohankumar-wh3uu 6 років тому +22

    Good explaination.
    But you should also check whether you are at leaf node or not when sum is equal to k.
    if(sum==k && p->left==NULL && p->right==NULL)
    then print the stack.

    • @sampathreddy024
      @sampathreddy024 4 роки тому +1

      I thought same

    • @souravroy4408
      @souravroy4408 4 роки тому

      Correct. And also pop the node off the stack and subtract the node value from the sum, so that we can move ahead with other path searches.

    • @sandeepvedavyas8701
      @sandeepvedavyas8701 4 роки тому

      @@souravroy4408 That will be automatically done if you ensure the leaf check condition

  • @naveendubey4412
    @naveendubey4412 4 роки тому

    Sir , Dil se bol rhaa hoon, best explanation. you have so much of knowledge

  • @NachiketJoshiTheBloodMage
    @NachiketJoshiTheBloodMage 6 років тому +8

    this is not a Root To Leaf PATH SUM Program... this will generate paths in the tree with a given sum not necessarily root to leaf

  • @jamesqiu6715
    @jamesqiu6715 7 років тому +3

    interesting to see that you correct your code at 07:46 on the fly ! good job!!!
    And it is modified to use Pre-Order traversal of the tree.

  • @domftw5475
    @domftw5475 2 роки тому

    Great explanation!

  • @satyaba1326
    @satyaba1326 3 роки тому

    Excellent explanation sir... Tq so much sir....

  • @hemanthn436
    @hemanthn436 4 роки тому

    good explaination sir, i understood your logic well but it's preorder traversal

  • @TheGreatOne428
    @TheGreatOne428 7 років тому +4

    the traversal that you are doing is definitely preorder ,not inorder

  • @harshvardhansingh780
    @harshvardhansingh780 3 роки тому

    Thank you sir

  • @ramakantasamal7482
    @ramakantasamal7482 6 років тому +3

    I think you have to pass the stack reference in the function arguments or else you lose the state of the stack at each recursive function call.

  • @aneeshbose6331
    @aneeshbose6331 6 років тому +4

    If there are multiple paths, the first one we get will pop all stack contents while printing. How do restore the data in the stack for printing other paths?

  • @ramakrishnakcr4417
    @ramakrishnakcr4417 3 роки тому

    looking forward more videos like this , ignore negative comments please, great work👌

  • @ayushthakur733
    @ayushthakur733 4 роки тому

    Great 🙌

  • @vitthalkadam
    @vitthalkadam 3 роки тому

    May be sum needs to be part of recursive call so that while unwinding we will have previous sum without current node ....

  • @nidhisingh7802
    @nidhisingh7802 4 роки тому

    kindly explain complexity in each questions .

  • @souravroy4408
    @souravroy4408 4 роки тому

    This code is not complete. We also need to check if the node is a leaf node , while checking the sum. Thank you for the wonderful explaination.

  • @coder_who_cooks3934
    @coder_who_cooks3934 7 років тому +2

    //by using this no need to take care of poping and decrement sum
    void getPath(BTnode* root, stack stk ,int sum , int temp)
    {
    if(root==NULL)
    return;
    stk.push(root->data);
    temp = temp+root->data;
    if(root->left ==NULL and root->right ==NULL)
    {
    if(temp==sum)
    {
    print(stk);
    }
    }
    getPath(root->left , stk , sum ,temp);
    getPath(root->right , stk , sum ,temp);
    }

  • @spamspam5741
    @spamspam5741 7 років тому

    The paths which don't include roots are not included I think. Please correct me if I am wrong (for example 14), I just saw that it was root to leaf paths,sorry, but could you also provide a code for my problem which doesn't necessarily include root.

  • @anjana1700
    @anjana1700 5 років тому

    Sir what if root node is not there in the required path,i.e. there is a path in left or right sub tree but does not include root?
    Please reply.Thank u in advance

    • @kumartatsat868
      @kumartatsat868 5 років тому +1

      The question says "root to leaf" path. So we have to include root essentially. The question you're asking is a different one. We have to print the path which includes root for sure.

    • @rajatbudania6181
      @rajatbudania6181 4 роки тому

      @@kumartatsat868 is it necessary that it will end on leaf? sum may be reached before leaf starting from root!

  • @shineychandan2074
    @shineychandan2074 7 років тому

    Could you please upload a video, how to return randomNode from a binary search tree?

  • @clashofclanss463
    @clashofclanss463 7 років тому +1

    sir can you upload graph tutorials???Thank you.....

  • @alperklc7782
    @alperklc7782 4 роки тому

    Can you show the iterrativ version of this function? Respects.

  • @shubhamjindal9214
    @shubhamjindal9214 6 років тому

    is this algo also valid for binary search tree ?

  • @bcjitu
    @bcjitu 5 років тому

    I guess, it's not root to leaf path sum problem.
    It will generate paths in the tree with a given sum not necessarily root to leaf.

    • @mohakchaudhary1981
      @mohakchaudhary1981 5 років тому

      It is root to leaf. But he has to add one condition of leaf node than print the stack.i think..😀🤔

  • @veerrajuyeleti8541
    @veerrajuyeleti8541 7 років тому

    sir could you publish a video on find a largest subtree having identical left and right subtrees

  • @TheGreatOne428
    @TheGreatOne428 7 років тому +4

    you are not checking for leaf node condition.this algorithm is not going to work

    • @vivekanandkhyade
      @vivekanandkhyade  7 років тому

      i will check it rohit...!

    • @mdsaif4696
      @mdsaif4696 7 років тому

      lets say we have another node (X) after 10 , then your function will be wrong ..

  • @Anand-zg6jv
    @Anand-zg6jv 2 роки тому +1

    int add=0;
    stackst;
    void roottoleaf(node* root,int k)
    {
    if(root==NULL)
    {
    return;
    }
    add=add+(root->data);
    st.push(root->data);
    if(add==k && root->left==NULL && root->right==NULL)
    {
    for(int i=0;i

  • @saranyakavuri5303
    @saranyakavuri5303 3 роки тому

    Sir, can you please provide the full code for the algorithm.

    • @educatorshub7860
      @educatorshub7860 3 роки тому

      LEETCODE #112
      /**
      * Definition for a binary tree node.
      * struct TreeNode {
      * int val;
      * TreeNode *left;
      * TreeNode *right;
      * TreeNode() : val(0), left(nullptr), right(nullptr) {}
      * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
      * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
      * };
      */
      class Solution {
      public:

      void helper(TreeNode* root, int targetSum,int sum,stack st,bool& ans)
      {
      if(root==NULL)
      {
      return;
      }
      sum+=root->val;
      st.push(root->val);
      if(sum==targetSum && root->left==NULL && root->right==NULL)
      {
      ans=true;
      }
      helper(root->left,targetSum,sum,st,ans);
      helper(root->right,targetSum,sum,st,ans);
      sum-=root->val;
      st.pop();



      }


      bool hasPathSum(TreeNode* root, int targetSum) {
      int sum=0;
      stack st;
      bool ans=false;
      helper(root,targetSum,sum,st,ans);
      return ans;
      }
      };

  • @mdsaif4696
    @mdsaif4696 7 років тому

    where to define stack ?? please provide full working code..

  • @bruteforceman2311
    @bruteforceman2311 4 роки тому

    Can anyone give me full code of this problem

  • @harshitchawla1689
    @harshitchawla1689 5 років тому +1

    This is not inorder

  • @datumyog9070
    @datumyog9070 6 років тому

    How about this one without using stack
    Tree * print_path_with_given_sum(Tree *root, int *sum, int final_sum)
    {
    if (root == NULL)
    return NULL;
    *sum = *sum + root->element;
    if (*sum == final_sum) {
    cout

  • @NareshKumar-dw9xp
    @NareshKumar-dw9xp 4 роки тому

    If anyone is interested in java implementation of above algorithm, please go through the link given below :)
    leetcode.com/problems/path-sum/discuss/605367/Using-Inorder-traversal-and-stack-In-java

  • @gattuchandrashekar5963
    @gattuchandrashekar5963 6 років тому

    This guy seems to be sleepy.