您的位置:首页 > 其它

LintCode 11. Search Range in Binary Search Tree

2018-02-12 11:00 225 查看

题目



思路

二叉树中序非递归遍历。

代码

"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
"""

class Solution:
"""
@param: root: param root: The root of the binary search tree
@param: k1: An integer
@param: k2: An integer
@return: return: Return all keys that k1<=key<=k2 in ascending order
"""
def searchRange(self, root, k1, k2):
# write your code here
res_list = []
stack = []
while root or len(stack):
while root:
stack.append(root)
root = root.left
if len(stack):
root = stack[-1]
stack.pop(-1)
if root.val >= k1 and root.val <= k2:
res_list.append(root.val)
root = root.right
return res_list
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: