Iam_Sandeep

K Sum Paths

Mar 14th, 2022
669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | None | 0 0
  1. # Following is the Binary Tree node structure.
  2. class BinaryTreeNode:
  3.     def __init__(self, data):
  4.         self.data = data
  5.         self.left = None
  6.         self.right = None
  7.  
  8. from collections import deque
  9. def kPathSum(root, k):
  10.     ans=[]
  11.     q=[]
  12.     def dfs(x):
  13.         if not x: return
  14.         q.append(x.data)
  15.         dfs(x.left)
  16.         dfs(x.right)
  17.         val=0
  18.         for i in range(len(q)-1,-1,-1):
  19.             val+=q[i]
  20.             if val==k:ans.append(q[i:][:])
  21.         q.pop()
  22.     dfs(root)
  23.     return ans
  24.                
  25.        
  26.  
Advertisement
Add Comment
Please, Sign In to add comment