Advertisement
Iam_Sandeep

863 · Binary Tree Path Sum IV

Aug 10th, 2022
1,536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.62 KB | None | 0 0
  1.  
  2. class Solution:
  3.     """
  4.    @param nums: a list of integers
  5.    @return: return an integer
  6.    """
  7.     def path_sum(self, nums: List[int]) -> int:
  8.         d=defaultdict(dict)
  9.         for num in nums:
  10.             level,pos,val = num//100,(num//10)%10,num%10
  11.             d[level][pos]=val
  12.         vis = set()
  13.         ans = 0
  14.         for lev in range(4,0,-1):
  15.             for pos in d[lev]  :
  16.                 if (lev,pos) in vis : continue
  17.                 for ht in range(lev,0,-1):
  18.                     vis.add((ht,pos))
  19.                     ans += d[ht][pos]
  20.                     pos = (pos+1)>>1
  21.         return ans
  22.  
  23.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement