forked from algorhythms/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch Range in Binary Search Tree.py
More file actions
41 lines (33 loc) · 999 Bytes
/
Search Range in Binary Search Tree.py
File metadata and controls
41 lines (33 loc) · 999 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""
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].
20
/ \
8 22
/ \
4 12
"""
__author__ = 'Daniel'
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
class Solution(object):
def searchRange(self, root, k1, k2):
ret = []
self.dfs(root, k1, k2, ret)
return ret
def dfs(self, root, k1, k2, ret):
if not root:
return
if root.val < k1:
self.dfs(root.right, k1, k2, ret)
elif root.val > k2:
self.dfs(root.left, k1, k2, ret)
else:
self.dfs(root.left, k1, k2, ret)
ret.append(root.val)
self.dfs(root.right, k1, k2, ret)