Guest User

Untitled

a guest
Oct 21st, 2019
84
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. struct Vertex {
  2. int value;
  3. int max_leaf;
  4. vector<Vertex*> children;
  5. };
  6.  
  7. void CountMaxLeaf(Vertex* vertex) {
  8. if (vertex == NULL) {
  9. return;
  10. }
  11. vertex->max_leaf = 0;
  12. for (auto child : vertex->children) {
  13. CountMaxLeaf(child);
  14. vertex->max_leaf = max(vertex->max_leaf, child->max_leaf);
  15. }
  16. vertex->max_leaf += vertex->value;
  17. }
  18.  
  19. void TransformTree(Vertex* vertex) {
  20. if (vertex == NULL) {
  21. return;
  22. }
  23.  
  24. for (auto child : vertex->children) {
  25. child->value = vertex->max_leaf - vertex->value;
  26. }
  27.  
  28. }
  29.  
  30. void ChangeTree(Vertex* root) {
  31. CountMaxLeaf(root);
  32. TransformTree(root);
  33. }
RAW Paste Data