TITLE: Prove that $A\cup B$ is connected.
QUESTION [7 upvotes]: Let $A$, $B$ $\subseteq \mathbb{R^n}$ be connected sets and suppose that
  $\overline{A} \cap B \ne \emptyset$. Prove that $A\cup B$ is
  connected.

My attempt: I've tried a proof by contradiction.
Suppose that $A\cup B$ is disconnected, e.g. $A\cup B = X\cup Y$, where $X, Y$ are disjoint, nonempty, and open in $A\cup B$.
Besides that, we have $A\cap X$ and $Y\cap A$ open in $A$, and that they cover A. But since A is connected by hypotesis, then we can suppose that  $A\cap X= \emptyset$. And that implies that $A \subseteq Y$.
So far, so good... but where do I get the contradiction? Where do I use that $\overline{A} \cap B \ne \emptyset$?
Any help to finish this proof would be appreciated!

REPLY [3 votes]: I once gave an answer to this question that is pretty similar (modulo notation) but you can also use the fact that 

$X$ is connected iff every continuous $f:X \to \{0,1\}$ (the latter in the discrete topology) is constant.

So take $f: A \cup B \to \{0,1\}$ and assume it's continuous. Then by connectedness of $A$, $f\restriction_A$ is constant (say with value $a \in \{0,1\}$) and likewise $f\restriction_B$ is constant with value $b \in \{0,1\}$. By continuity of $f$ we know:
$$f[\overline{A}] \subseteq \overline{f[A]} = \{a\}$$
while of course $f[B]=\{b\}$. But as $\overline{A} \cap B \neq \emptyset$ it follows that $a=b$ and $f$ is constant and thus $A \cup B$ is connected.