Mangrove roots stand in salt water that would kill an ordinary tree. Most of the defense is at the root surface. The membranes there exclude a large share of the salt as water goes in, a living filter. The water that arrives in the leaves is still brackish, but it is far less salty than the tide.
Per Scholander measured that desalination in 1968, and P. B. Tomlinson's The Botany of Mangroves is the fuller natural history. Species do not all use the same second step. Avicennia has salt glands on the leaf and excretes brine you can see as crystals. Other mangroves lean harder on exclusion and on sacrificing old leaves. Standing in the sea is several inventions, not one.