Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- /*
- Problem Link: https://leetcode.com/problems/search-in-a-binary-search-tree/
- Language:
- Swift
- Params:
- root: Custom(TreeNode) data(class) type;
- val: Int;
- Return:
- Custom(TreeNode) data(class) type; return a TreeNode object.
- Complexity analysis:
- Space: Constant space
- Time: O(log n)
- */
- func searchBST(_ root: TreeNode?, _ val: Int) -> TreeNode? {
- var curr_node = root
- while curr_node != nil {
- if curr_node?.val == val {
- return curr_node
- }
- if curr_node!.val >= val {
- curr_node = curr_node?.left
- } else {
- curr_node = curr_node?.right
- }
- }
- return nil
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement