-
-
Save parcar/3c8aa791c9881d099ba6298dddca9e1d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.