$H$ $:=$ MAKE-HEAP()
INSERT($H$, $n$)
INSERT($H$, $n-1$)
INSERT($H$, $-\infty$)
EXTRACT-MIN($H$)
for $k$ from $n-2$ down to $1$:
INSERT($H$, $k$)
INSERT($H$, $\infty$)
INSERT($H$, $-\infty$)
EXTRACT-MIN($H$)
DELETE($H$, $\infty$)
$1 \mapsto 2 \to 3$
$2 \mapsto 1 \to 4 \to 5$
$3 \mapsto 1 \to 6 \to 7$
$4 \mapsto 2$
$5 \mapsto 2$
$6 \mapsto 3$
$7 \mapsto 3$
$\begin{pmatrix} 0 & 1 & 1 & 0 & 0 & 0 & 0\\ 1 & 0 & 0 & 1 & 1 & 0 & 0\\ 1 & 0 & 0 & 0 & 0 & 1 & 1\\ 0 & 1 & 0 & 0 & 0 & 0 & 0\\ 0 & 1 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 1 & 0 & 0 & 0 & 0\\ 0 & 0 & 1 & 0 & 0 & 0 & 0 \end{pmatrix}$
$(B B^T )_{ij}= \Sigma_{k=1}^{m}{B_{ik}B^{T}_{kj}} = \Sigma_{k=1}^{m}{B_{ik}B_{jk}}$ Note that, for given $k$, there are unique $i$ and $j$ of $1$ and $-1$ respectively.
Thus, for $i \neq j$, $$(B B^T )_{ij} = \Sigma_{k=1}^{m}{B_{ik}B_{jk}} = -(\text{# of edges connect vertices }i \text{ and } j)$$
And, for $i=j$, $$(B B^T )_{ij} = \Sigma_{k=1}^{m}{B_{ik}B_{ik}} = (\text{# of edges starts or ends in vertex }i)$$
Since if $u$ and $v$ are satisfying $\delta(u, v)$ equals to diameter of tree $T = (V, E)$, then both $u$ and $v$ are leaves, and there is only one path connecting $u$ and $v$. So we have the relation
$\text{diameter of } T = \max_{u, v \in V}{\delta(u, v)}$
$= \max_{v \in V}{ [(1^\text{st}\text{ highest height of }v\text{'s subtree}) + (2^\text{nd}\text{ highest height of }v\text{'s subtree})] }$
Current_Max $:=$ $0$
Tree-Diameter($T$):
Evaluate-Height($T$, $T$.root)
return Current_Max
Evaluate-Height($T$, $r$):
if $r$ has no children, then return $0$
let $H$ be new array, and its size equals to number of children of $r$
for $v \in \{ \text{children of }r \}$
$H[v] := $ Evaluate-Height($T$, $v$) $+$ $1$
$h_1 := \max H$
if $|H| > 1$:
$ h_2 := \max (H - \{h_1\})$
else:
$h_2 := 0$
if $h_1 + h_2 >$ Current_Max:
Current_Max $:= h_1 + h_2$
return $h_1$
The algorithm traveals entire tree, visits and allocates memory for storing height of each node once. $\implies$ The time complexity is $O(|V|)$.