8 Lines Simple DFS Python

  • -1
    def trimBST(self, root, L, R):
        def dfs(node):
            if not node:        return None
            node.left = dfs(node.left)
            node.right = dfs(node.right)
            if node.val < L:    return node.right
            if node.val > R:    return node.left
            return node
        return dfs(root)

Log in to reply

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