Path Sum 2 in Binary Tree | Leetcode 113 Solution

preview_player
Показать описание




.
.
.
Happy Programming !!! Pep it up 😍🤩
.
.
.
#pepcoding #code #coder #codinglife #programming #coding #java #freeresources #datastrucutres #pepcode #competitive #competitiveprogramming #softwareengineer #engineering #engineer
Рекомендации по теме
Комментарии
Автор

Explained very well! Was able to code it on my own without seeing the video further :)

ayushidalal
Автор

Good explanation of why we have to create a new list every time (Shallow cloning concept).

chhaviagarwal
Автор

i have also used the same approach, but the same code fails for nodes with negative values, can u please confirm whether this code runs for -ve values as well ?

DurgaShiva
Автор

What will be the time and space complexity?

manantyagi
Автор

Nice video. Please do a video on path sum 3 also with complexities.

pavantejak
Автор

Adding(line - 27) and removing(line - 32) from smallAns list is redundant. Without that also code will work if we pass the updated list in function call.

class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
def helper(root, targetSum, path):
if not root:
return
if root.left is None and root.right is None:
if targetSum - root.val == 0:
self.answer.append(path + [root.val])
helper(root.left, targetSum - root.val, path + [root.val])
helper(root.right, targetSum - root.val, path + [root.val])
self.answer = []
helper(root, targetSum, [])
return self.answer

TheShantanu
welcome to shbcf.ru