Python, Recursive Tree Traversal


  • 0
    P
    def trimBST(self, root, L, R):
        """
        :type root: TreeNode
        :type L: int
        :type R: int
        :rtype: TreeNode
        """
        if root is not None:
            if root.val > R: # ignore the right subtree
                return self.trimBST(root.left, L, R)
            elif root.val < L: # ignore the left subtree
                return self.trimBST(root.right, L, R)
            else:
                root.left = self.trimBST(root.left, L, R)
                root.right = self.trimBST(root.right, L, R)
                return root
    

Log in to reply
 

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