Skip to content

Instantly share code, notes, and snippets.

@zzandland
Created December 10, 2019 05:28
Show Gist options
  • Save zzandland/f0b7bd0187f624626f10b5cf2afad8d0 to your computer and use it in GitHub Desktop.
Save zzandland/f0b7bd0187f624626f10b5cf2afad8d0 to your computer and use it in GitHub Desktop.
Count percentile
class Node:
def __init__(self, n: int):
self.val = n
self.left = self.right = None
def percentageUnderNum(root: Node, threshold: int, depth: int) -> float:
def helper(node: Node, count: int, total: int, curDepth: int) -> [int]:
output = [count, total]
if not node or curDepth > depth:
return output
if node.val < threshold:
output[0] += 1
left = helper(node.left, count, total + 1, curDepth + 1)
right = helper(node.right, count, total + 1, curDepth + 1)
return [left[0] + right[0], left[1] + right[1]]
rootOutput = helper(root, 0, 0, 0)
return rootOutput[0] / rootOutput[1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment