Insert into a Binary Search Tree (Leetcode 701)

Problem Link: https://leetcode.com/problems/insert-into-a-binary-search-tree/

class Solution:
    def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:

        if(root is None):
            return TreeNode(val)
        curr=root
        
        while(1):
            if(root.val<val):
                if(root.right is None):
                    root.right=TreeNode(val)
                    break
                else:
                    root=root.right
            else:
                if(root.left is None):
                    root.left=TreeNode(val)
                    break
                else:
                    root=root.left
        return curr

Last updated