938
设置判断层次的数量
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def rangeSumBST(self, root: TreeNode, low: int, high: int) -> int:
res = 0
queue = []
queue.append(root)
while len(queue) > 0: #判断是否遍历完
size = len(queue) #判断该层次的数量
while size > 0: #遍历该层次的数量
temp = queue.pop()
if temp.val <= high and temp.val >=low:
res += temp.val
if temp.left:
queue.append(temp.left)
if temp.right:
queue.append(temp.right)
size -= 1
return res