Skip to content

Instantly share code, notes, and snippets.

@parcar
Created August 15, 2019 17:26
Show Gist options
  • Select an option

  • Save parcar/3c8aa791c9881d099ba6298dddca9e1d to your computer and use it in GitHub Desktop.

Select an option

Save parcar/3c8aa791c9881d099ba6298dddca9e1d to your computer and use it in GitHub Desktop.
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def searchBST(self, root, val):
"""
:type root: TreeNode
:type val: int
:rtype: TreeNode
"""
ms = []
if(root is None):
return
ms.append(root)
while(len(ms)):
curr = ms.pop()
if (curr.val == val):
return curr
elif (curr.val > val and curr.left):
ms.append(curr.left)
elif (curr.val < val and curr.right):
ms.append(curr.right)
else:
return
@parcar
Copy link
Author

parcar commented Aug 15, 2019

Runtime: 64 ms, faster than 85.33% of Python online submissions for Search in a Binary Search Tree.
Memory Usage: 15.9 MB, less than 10.71% of Python online submissions for Search in a Binary Search Tree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment