mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 12:16:14 -04:00
33 lines
643 B
Python
33 lines
643 B
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
# author: bt3gl
|
|
|
|
|
|
def search_bst_recursive(root, val):
|
|
|
|
if root is None or root.val == val:
|
|
return root
|
|
|
|
if val > root.val:
|
|
return search_bst_recursive(root.right, val)
|
|
|
|
else:
|
|
return search_bst_recursive(root.left, val)
|
|
|
|
|
|
|
|
def search_bst_iterative(root, val):
|
|
|
|
while root:
|
|
|
|
if root.val == val:
|
|
break
|
|
|
|
if root.val < val:
|
|
root = root.right
|
|
|
|
else:
|
|
root = root.left
|
|
|
|
return root
|