Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find all the keys of tree in range k1 to k2. i.e. print all x such that k1<=x<=k2 and x is a key of given BST. Return all the keys in ascending order.
Example
If k1 = 10 and k2 = 22, then your function should return [12, 20, 22].
"""Definition of TreeNode:class TreeNode: def __init__(self, val): self.val = val self.left, self.right = None, None"""classSolution:""" @param root: The root of the binary search tree. @param k1 and k2: range k1 to k2. @return: Return all keys that k1<=key<=k2 in ascending order. """defsearchRange(self,root,k1,k2):# write your code hereresult=[]ifroot==None:returnresultifroot.left!=None:left_result=self.searchRange(root.left,k1,k2)else:left_result=[]result=result+left_resultifroot.val>=k1androot.val<=k2:result.append(root.val)ifroot.right!=None:right_result=self.searchRange(root.right,k1,k2)else:right_result=[]result=result+right_resultreturnresult