The Messenger of Allah ﷺ said, "Among the deeds and good works that continue to reach a believer after his death are knowledge he taught and spread, a righteous child he left behind, a copy of the Quran he passed on as inheritance, a mosque he built, a lodging he built for travelers, a river he caused to flow, or charity he gave from his wealth while in good health during his lifetime. These will reach him after his death."
حدثنا محمد بن يحيى حدثنا محمد بن وهب بن عطية حدثنا الوليد بن مسلم حدثنا مرزوق بن أبي الهذيل حدثني الزهري حدثني أبو عبد الله الأغر
قال رسول الله ﷺ إن مما يلحق المؤمن من عمله وحسناته بعد موته علما علمه ونشره وولدا صالحا تركه ومصحفا ورثه أو مسجدا بناه أو بيتا لابن السبيل بناه أو نهرا أجراه أو صدقة أخرجها من ماله في صحته وحياته يلحقه من بعد موته
[Its chain is daif. Bayhaqi narrated it in Shuab al-Iman, Ibn Khuzayma graded it sahih, and Munziri graded it hasan. Walid did not clearly state hearing it directly throughout the chain, and the majority weakened his teacher.]