Advertisement
halaluddin

Day14-Apr-Search_in_a_BST-Recursive

Apr 14th, 2022
1,247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.69 KB | None | 0 0
  1. class Solution {
  2.    
  3.     /*
  4.         Problem Link: https://leetcode.com/problems/search-in-a-binary-search-tree/
  5.         Language:
  6.             Swift
  7.         Params:
  8.             root: Custom data(class) type;
  9.             val: Int;
  10.         Return:
  11.             Custom data(class) type; return a class object.
  12.         Complexity analysis:
  13.             Space: O(Possible reached recursion depth) -> O(log n)
  14.             Time: O(log n)
  15.     */
  16.    
  17.     func searchBST(_ root: TreeNode?, _ val: Int) -> TreeNode? {
  18.         if let curr_node = root {
  19.             return curr_node.val == val ? curr_node : searchBST(curr_node.val >= val ? curr_node.left : curr_node.right, val)
  20.         }
  21.         return nil
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement