A closed-form approach to Bayesian inference in tree-structured graphical models