Simple recursive - python


  • 0
    S
    class Solution(object):
        def trimBST(self, root, L, R):
            """
            :type root: TreeNode
            :type L: int
            :type R: int
            :rtype: TreeNode
            """
            if root:
                if L <= root.val <= R:
                    root.left  = self.trimBST(root.left, L, R)
                    root.right = self.trimBST(root.right,L, R) 
                    return root
                if root.val < L:
                    return self.trimBST(root.right,L, R) 
                elif root.val > R:
                    return self.trimBST(root.left,L, R)
            return None
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.