Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node's value equals the given value. Return the subtree rooted with that node. If such node doesn't exist, you should return NULL.
this is what i get for my code:
class Solution:
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
if root == None:
return None
elif root.val == val:
return root
elif root.val>val:
root = self.searchBST(root.left,val)
else:
root = self.searchBST(root.right,val)
which seems to output None. but if replace the line
root = self.searchBST(root.left,val)
with
return self.searchBST(root.left,val)
same with root.right
then it works. I am not too good at recursion but how can we do
return self.searchBST(root.left,val)
doesnt searchBST(..) return a TreeNode so dont we have to put the treenode in the variable root?