fork(1) download
  1. class Solution:
  2. def countSmaller(self, nums):
  3. class Node:
  4. def __init__(self, val):
  5. self.val = val
  6. self.left = None
  7. self.right = None
  8. self.count = 0
  9.  
  10. def bst_insert(root, val, result, count):
  11. if not root:
  12. root = Node(val)
  13. result.append(count)
  14. return root
  15. if val > root.val:
  16. root.right = bst_insert(root.right, val, result, root.count + 1 + count)
  17. else:
  18. root.count += 1
  19. root.left = bst_insert(root.left, val, result, count)
  20. return root
  21.  
  22. root = None
  23. result = []
  24. for num in reversed(nums):
  25. if not root:
  26. root = Node(num)
  27. result.append(0)
  28. else:
  29. bst_insert(root, num, result, 0)
  30. return result[::-1]
Success #stdin #stdout 0.04s 64388KB
stdin
Standard input is empty
stdout
Standard output is empty