add common funtion for tree problems