Python simple recursion


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

Log in to reply
 

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