  In this paper we study several problems concerning the number of
homomorphisms of trees. We give an algorithm for the number of homomorphisms
from a tree to any graph by the Transfer-matrix method. By using this algorithm
and some transformations on trees, we study various extremal problems about the
number of homomorphisms of trees. These applications include a far reaching
generalization of Bollob\'as and Tyomkyn's result concerning the number of
walks in trees.
  Some other highlights of the paper are the following. Denote by $\hom(H,G)$
the number of homomorphisms from a graph $H$ to a graph $G$. For any tree $T_m$
on $m$ vertices we give a general lower bound for $\hom(T_m,G)$ by certain
entropies of Markov chains defined on the graph $G$. As a particular case, we
show that for any graph $G$,
$$\exp(H_{\lambda}(G))\lambda^{m-1}\leq\hom(T_m,G),$$ where $\lambda$ is the
largest eigenvalue of the adjacency matrix of $G$ and $H_{\lambda}(G)$ is a
certain constant depending only on $G$ which we call the spectral entropy of
$G$. In the particular case when $G$ is the path $P_n$ on $n$ vertices, we
prove that $$\hom(P_m,P_n)\leq \hom(T_m,P_n)\leq \hom(S_m,P_n),$$ where $T_m$
is any tree on $m$ vertices, and $P_m$ and $S_m$ denote the path and star on
$m$ vertices, respectively. We also show that if $T_m$ is any fixed tree and
$$\hom(T_m,P_n)>\hom(T_m,T_n),$$ for some tree $T_n$ on $n$ vertices, then
$T_n$ must be the tree obtained from a path $P_{n-1}$ by attaching a pendant
vertex to the second vertex of $P_{n-1}$.
  All the results together enable us to show that $$
|\End(P_m)|\leq|\End(T_m)|\leq|\End(S_m)|, $$ where $\End(T_m)$ is the set of
all endomorphisms of $T_m$ (homomorphisms from $T_m$ to itself).
