Path Sum

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

КОМЕНТАРІ • 51

  • @anshikagarg7383
    @anshikagarg7383 4 роки тому +9

    Your logics and explanations are so clear Kevin.They are of great help for the beginners.A big THANK YOU :)

  • @parmarhitendrasinh4504
    @parmarhitendrasinh4504 4 роки тому +8

    Wow that sound of the keystrokes :)

  • @sanajahan1275
    @sanajahan1275 5 років тому +15

    Hi, @Kevin Naughton Jr. your videos are extremely simple and easy to understand. Thanks for making such videos. Also, if at the end of each problem, if you could talk about the time complexity of your approach, that could also help a lot.

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

      SANA JAHAN thanks Sana I really appreciate it! In more recent videos I talk about time and space complexity at the end :)

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

      @@KevinNaughtonJr Hi, would you please also do some videos on sliding window concept related problems:)? Thanks.

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

      @@sanajahan1275
      See medium article by Saurabh Medhapati .it is like gem for sliding window

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

      ​@@goodpeople7615 can you pls send the link I can't find it out

  • @Porsche911Dream
    @Porsche911Dream 4 роки тому +7

    You're awesome man

  •  5 років тому +8

    Cool video
    two advices for a more readable code
    1. Given that you use return after the if, It is not needed to uses else if
    2. You can use return sum === root.val instead of sum -val === 0

  • @ashishmalik4799
    @ashishmalik4799 6 років тому +11

    As you mentioned in the video that most of the problems on trees can be solved by Recursion. Is Recursion really the best method available to solve these type of questions??

    • @KevinNaughtonJr
      @KevinNaughtonJr  6 років тому +36

      There's always a tradeoff...recursive solutions are great because it oftentimes allows a complex problem to be broken down and solved rather simply (in terms of short, readable code). The downside of recursion is that it can use a lot of memory due to the recursive calls on the stack and therefore might overflow for certain inputs. It's a idea good to mention the pros and cons of using recursion during your interviews :)

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

      Does tail-call recursion mitigate the stack overflow issue with recursion? And if so, would there then be any advantage of iterative over recursive, if recursion's main fault was offset?

  • @ivanchl
    @ivanchl 5 місяців тому

    The best and easiest solution! Thank you

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

    This logic is awesome !!! I was like writing lot of if conditions n loops.... How can you think of such crisp solution ??

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

    Nice solution. Though we can modify 2nd if else block I believe.
    else if (root.left == null && root.right == null) return (sum - root.val == 0)
    When node doesnt have a child, we are unnecessarily increasing a call stack.

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

    I was trying to solve it as BFS and Dynamic Programming solution.. I thought too much tho

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

    Best solutions in the net. hands down

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

    Difference in logic in dp when you have to return true/false vs when you return some structure

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

    Great explanation! Thanks!

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

    This seems like your just going down the left and right is that correct

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

    What is the time and space complexity of this method? It seems like time is O(n) but I'm not sure about space... is the space complexity O(h) where h is the height of the tree? Thanks! :)

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

      Auxiliary space complexity due to recursion on the worst case is O(n) and I think no other extra space is used.

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

    in tree problems we always use recursion and then they ask me over the phone how to solve it recursively, and I felt like F*************************n lol

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

    nice & clean explanation
    thanks!

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

    Clean and nice explanation. Thanks.

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

    is there any iterative approach for this question?

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

      why does no one know how to do the iterative approach to this problem.

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

    thanks dear bro

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

    Can I reduce the recursion by adding a negative check on sum?

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

    Can someone explain why in the return statement there is || operator and not && . We want both left and right to be called right?

  • @ЏонМастерман
    @ЏонМастерман 4 роки тому

    hasPathSum(root.left, ...) || hasPathSum(root.right, ... )
    I don't know what does this code call, recursive call on both branches or something else?

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

      Ya, it calls on both the branches. Just think as root.left has become new root and root.val-sum is new sum. And if either one of these returns true means there is a path.

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

      It keeps go down to the end see if one end return true....

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

    You are a God

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

    What is the time complexity of this algorithm? I have trouble finding the time complexity of recursive algorithms. I assume that since the recursive calls goes through n levels where n is the height of the algorithm, so it'd be O(log n) I think?

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

      I believe the runtime is O(N) where N is the number of nodes in the tree. It helps me to think about it logically instead of in terms of code so maybe that will help you too. For this problem, I think to myself, "what's the worst possible scenario" to which I think the worst possible scenario is that we go through checking all the nodes in the tree but still don't manage to find a path that adds to the desired sum. So with that logic I say it's O(N). Does that make sense?

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

      @@KevinNaughtonJr Okay, that makes more sense. Would it better to represent the number of nodes as a singular variable (N) or as a representation of the tree's height in an actual interview?

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

      @@nealpatel7696 In my interview experience it's typical to use N to describe the number of nodes in a tree :). LMK if you have any other questions Neal!

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

      @@KevinNaughtonJr One last one! This isn't necessarily related to this problem, though. I like using Python for interviews because of its conciseness. Since in Python, lists are used in place of arrays, can you just give a general time complexity of an algorithm regardless of how it's implemented in the specific programming language? I believe under the hood, Python lists are just vectors in C and also similar to ArrayLists in Java.

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

      @@nealpatel7696 Yes in all my interviews I give the general time complexity of the algorithm regardless of the language...however, you should understand the language you're choosing to use. What I mean by that is understand how things work under the hood. So in Java is I use the "contains" method I need to understand that that will perform a linear scan of my elements looking for a specific element. My point being that you can degrade your runtime if you don't understand the consequences of using certain functions in your language...so a solution that I thought was O(n) might become O(n * m) because of a function I choose to use. I hope that makes sense, if it doesn't lmk and I can explain more :)

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

    really simple explanation ver nic

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

      Thanks so much happy to hear the explanations are helpful!

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

    class Solution
    {
    public boolean hasPathSum(TreeNode root, int k)
    {

    if(root==null)
    {
    return false;
    }
    if(root.left==null && root.right==null && k-root.val==0)
    {
    return true;
    }

    boolean leftCall=hasPathSum(root.left,k-root.val);
    boolean rightCall=hasPathSum(root.right,k-root.val);

    return leftCall || rightCall;
    }
    }

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

    ty

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

    Thank you for the video. For anyone looking for a python solution
    def path_sum(root, value):
    if not root:
    return
    stack = []
    stack.append([root, value-root.data])
    while value != 0 and stack:
    node, rsum = stack.pop()
    if rsum == 0:
    return True
    if node.right:
    stack.append([node.right, rsum - node.right.data])
    if node.left:
    stack.append([node.left, rsum - node.left.data])
    return False

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

    THANK U, NEXT