Path Sum II

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

КОМЕНТАРІ • 85

  • @KevinNaughtonJr
    @KevinNaughtonJr  5 років тому +14

    If there's anything I can do to help you guys be sure to leave me a comment and I'll see what I can do!

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

      Hello Kevin! I think I have another problem suggestion for you, Could you please solve LeetCode #127 Word Ladder? This one actually appeared in a Mock Interview I did yesterday with a Facebook Engineer. He also asked me to solve #66 but that one is trivial (or is it?). Let me know What do you think, and have a great day.

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

      @@jlecampana I'll check them out thanks for the suggestions! I hope you ave a great day too!

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

      Hi Kevin. I want to become a patron but I had some doubts regarding the tier I want to choose. Please let me know if I can talk to you about it.
      Thanks.

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

      hey kevin ,your solution is so clear and nice ,but can you optimize it to be faster ,currently 95 % of other solutions on leetcode are faster.we appreciate your effort and support .and second what are the complexity for your code .you didn't mentioned it .

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

      Thanks for your videos. It really helps to get an idea of the problem solving. I would like to have a request to go over the different approaches so that we can think of different trade offs before coding the actual problem as it is a key aspect for an interview.

  • @adamk3486
    @adamk3486 5 років тому +38

    Thanks for posting and great video. Small optimization is that you could keep the same list and backtrack when a solution is not correct instead of creating a new list at every iteration. For people wondering it would look something like:
    if(root == null) return;
    current.add(root.val);
    if(sum == root.val && root.left == null && root.right == null)
    paths.add(new ArrayList(current);
    findPaths(root.left, sum - root.val, current, paths);
    findPaths(root.right, sum - root.val, current, paths);
    current.remove(current.size() - 1); //if we hit null recurse back up through stack and remove elements in current list
    Thanks for the content and keep the videos coming!

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

      Anytime Adam! So smart thanks for awesome optimization!!!

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

      but for some reason you need to put return after removing last element from stack or it won't work,not sure why

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

      I wonder why there's no return statement in the if condition.

    • @adamk3486
      @adamk3486 2 роки тому +1

      @@studyaccount794 If you have a return in the if statement then you don't properly remove elements from the list. Lets look at a simple example.
      1
      / \
      2 2
      Where the target sum = 3. Also to note here that we are subtracting from sum based on the above code.
      Iteration 1: list = [1] sum = 3
      Iteration 2: list = [1,2] sum = 2 == root.val. Add this list to the results list and return.
      Now we moved back up the call stack to node 1, but we never recursed back through our list.remove() part so there is an extra 2.
      Iteration 3: list = [1,2] sum = 3
      Iteration 4: list = [1,2,2] sum = 2 == root.val. Add this to the results list and return.
      End result [[1,2],[1,2,2]]. Which is incorrect.
      Hope this helps.

  • @AviRecital
    @AviRecital 3 місяці тому

    I don't know if you're still doing these videos but keep them coming. Concise, precisely explained. Ty.

  • @shivamdhir640
    @shivamdhir640 2 роки тому +5

    Hi Kevin, first of all , awesome explanation, can you please explain why we create a new ArrayList for every recursive call ?

  • @arunimamangal2733
    @arunimamangal2733 3 роки тому +3

    Damn you make every problem feel so simple.

  • @shubhammittalSHM
    @shubhammittalSHM 5 років тому +9

    Absolutely incredible! Can you please solve some DP and linked lists questions as well? Keep up the good work.

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 років тому +3

      You've got it and thanks Shubham I appreciate it!!!

  • @rishikeshrai6686
    @rishikeshrai6686 4 роки тому +3

    Nice explanation sir .
    I have a doubt i.e during the recursive call what happen if we pass current in parameter instead of new ArrayList(current)

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

    Thanks for sharing this solution. Could you also please share the time and space complexity for solutions and talk about trade offs.

  • @umabharati9911
    @umabharati9911 4 роки тому +3

    Kevin, I just can't tell you how helpful you are to me! You are making every question on leetcode easier with your channel here. Would you please do the same for the hard problems too?

  • @AnkitSingh-lb5ct
    @AnkitSingh-lb5ct Рік тому

    Goddamn kevin, that copy current list was where I lacked(I was passing the original one). Thanks for the info legend.

  • @yskida5
    @yskida5 Рік тому

    Anyone know if the person who made the intro song has another channel or has other online presence? Loved the beat & great video!

  • @driveb6591
    @driveb6591 4 роки тому +6

    Great video :)
    What's the time complexity??

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

    Thanks Kevin... You explain the things in easiest way. Could you please explain non recursive approach as well to this problem?

  • @monishalurgowdru1328
    @monishalurgowdru1328 4 роки тому +2

    Why do you create a copy of list current in recursive call and what does that list contain?

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

    What will be time complexity to make a new arraylist from already present arraylist?

  • @Augustus1003
    @Augustus1003 2 роки тому +1

    could anyone please explain why we should not remove the nodes from current list after the two recursive calls?, suppose if the root to leaf path sum of left child is not equal to the target in that case we have to remove that left leaf node from the current list and then check for the right leaf node.

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

    This solution is definitely derived after lot of attempts... Amazing solution

  • @DeepakSingh-fi9tx
    @DeepakSingh-fi9tx 5 років тому +2

    Nice Explanation. Keep these high quality videos coming. :)

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

    Hi Kevin, thanks for posting the videos. Can you do a video on Longest Duplicate Substring?

  • @hermanyniu2292
    @hermanyniu2292 5 років тому +4

    Hi Kevin, I have a question regarding backtracking. I've seen other code that have a line like "current.remove(current.length - 1)", and they also passed. I didn't fully understand why that line would be necessary in that case, and how that is different from your method.

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

      so its like a take or not take ,so when u perform adding element to current solution your taking ,then u perform not take by removing the element

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

      The code with backtracking runs faster than Kevin's code, cause it only creates a new instance of ArrayList when the list needs to be added to result.

  • @adiviswanath
    @adiviswanath 4 роки тому +2

    Why do you pass a copy of the current list and not the same list ?

  • @jlecampana
    @jlecampana 5 років тому +2

    It's cool that you usually do problems I tried recently myself, Here's my solution to this one in C++, it's a basic DFS I think.
    void pathSum(TreeNode *root, int currSum, int target, vector path, vector &paths) {
    if (root == NULL)
    return;
    if ((root->val + currSum) == target && (root->left == NULL && root->right == NULL)) {
    path.push_back(root->val);
    paths.push_back(path);
    } else {
    path.push_back(root->val);
    pathSum(root->left, root->val + currSum, target, path, paths);
    pathSum(root->right, root->val + currSum, target, path, paths);
    }
    }
    vector pathSum(TreeNode* root, int sum) {
    vector paths;
    pathSum(root, 0, sum, vector(), paths);
    return paths;
    }

  • @alanl4885
    @alanl4885 5 років тому +2

    I see path sum i and ii. when's III coming? :)

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

    why you passed new copy of arraylist instead of passing original in recursion ?

  • @jyotikumari-lp1qh
    @jyotikumari-lp1qh 3 роки тому +1

    Bring some series of graph questions

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

    Is the space complexity is ON² ?

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

    As someone who has recently started doing leetcode/hackerrank, what advice do you have to improve my abilities (ie. What are some important data structures, algorithms, process to tackle problem)

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 років тому +4

      Hey Afzal, I think the biggest thing I'd suggest (besides just practicing) is to try and get good at recognizing what ds / algos are useful for what problems...understand when a hash map, stack, heap, etc is useful in certain situations and why! Let me know if you have any other questions or if there's anything I can do to help you!

    • @afzalchishti9092
      @afzalchishti9092 5 років тому +2

      thanks so much, and thank you for such great content, really goes a long way to help people out!

    • @KevinNaughtonJr
      @KevinNaughtonJr  5 років тому +2

      @@afzalchishti9092 Anytime Afzal that's all I want to do so I'm happy to hear it's helping!!!

  • @huanjessica8189
    @huanjessica8189 8 місяців тому

    I'm not sure why output becomes [[5,4,11,2],[5,4,8,4,5]] when I use backtracking and still keep return in if(root.val == target && root.left == null && root.right ==null) this condition. If I remove return statement, the output is correct.

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

    Note: make sure to path new ArrayLIST(current) in recursion

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

    What does using the 'new' keyword do when passed as a parameter?

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

    Umm would it still work without current list? If not, could you please explain why we need 'current'?
    Thanks!

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

      I was thinking of the same thing. but as we need output in the form of List we need to have an intermediate List.

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

    Hi Kevin ! Can you make one video for solving Path Sum III I tried to understand the O(n) solution but could not get any

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

    Could you solve LeetCode 523 (Continuous Subarray Sum)?

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

      I'll see what I can do thanks for the suggestion!

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

    what is the complexity ?

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

    Amazon boy!!

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

    what's the time complexity for this??

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

    Awesome

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

    I am sorry to say Kevin bro , I am close to exhausting all your medium level problems.. yup its that good.. upload more ....

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

    would you be able to implement this iteratively using dfs?

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

    I can't understand why we can't pass the same list. Why does that fail?
    Can anybody please help me understand.

  • @yanchengpan543
    @yanchengpan543 3 роки тому +1

    why we need a unique list every recursion

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

    What about complexities?

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

    I think u should use a pen to make students understand

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

    Can this be resolved by Dynamic Programming

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

    Nice video!!

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

    THANK YOU!!!

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

    Pls make a video on path sum 3

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

    Lmao nice thumbnail

  • @IagoDoval1
    @IagoDoval1 4 місяці тому

    I came here from chatgpt hahaha

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

    Completing 800 problems on Leetcode prints you Facebook/Google offer. Practice 24*7. Don't eat, drink, poop, fart, sleep.

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

    WHAT IS WRONG WITH THE FOLLOWING CODE???
    void dfs(TreeNode* node,vector&ans,vector&curr,int target)
    {

    if(node==NULL)
    return;
    curr.push_back(node->val);
    if(node->left==NULL && node->right==NULL && node->val==target)
    {
    ans.push_back(curr);
    return;
    }

    dfs(node->left,ans,curr,target-node->val);
    dfs(node->right,ans,curr,target-node->val);
    }

    vector pathSum(TreeNode* root, int target)
    {
    vectorans;
    vectorcurr;
    dfs(root,ans,curr,target);
    return ans;
    }

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

    Am I deaf?

  • @JohnWick-zc5li
    @JohnWick-zc5li 5 років тому

    thanks as usual.. pls no recursion.

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

      Haha why no recursion?!?!

    • @DeepakSingh-fi9tx
      @DeepakSingh-fi9tx 5 років тому

      Recursion is easiest way to solve this problem. But resursion is difficult to understand & easy to write (Only after understanding :P). That's why I am here :).

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

    Please use some drawing board, whiteboard etc to explain the solution. u always talk verbally about all of these complex problems, no fresher can understand this.