700. Search in a Binary Search Tree
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def searchBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
cur = root
while cur:
if cur.val == val:
return cur
elif cur.val < val:
cur = cur.right
else: # if cur.val > val:
cur = cur.left
return cur
'LeetCode > 주제별 보충' 카테고리의 다른 글
Graphs(Union Find): 547. Number of Provinces (0) | 2024.11.18 |
---|---|
BST: 450. Delete Node in a BST (0) | 2024.11.18 |
Tree: 1448. Count Good Nodes in Binary Tree (2) | 2024.11.15 |
Tree: 104. Maximum Depth of Binary Tree (2) | 2024.11.15 |
Bit: 338. Counting Bits (3) | 2024.11.09 |