Guest User

Untitled

a guest
Feb 23rd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. using System;
  2.  
  3. class Solution
  4. {
  5. public class Node
  6. {
  7. public int cost;
  8. public Node[] children;
  9. public Node parent;
  10. }
  11.  
  12. public static int getCheapestCost(Node rootNode)
  13. {
  14. if(rootNode == null)
  15. {
  16. return 0;
  17. }
  18.  
  19. var cost = rootNode.cost ;
  20. var children = rootNode.children;
  21.  
  22. if(children == null || children.Length == 0)
  23. {
  24. return cost;
  25. }
  26.  
  27. int minPathSum = int.MaxValue;
  28.  
  29. foreach(var child in children)
  30. {
  31. // minPathSum = Math.Min(minPathSum, getCheapestCost(child));
  32. var minPathChild = getCheapestCost(child);
  33. minPathSum = minPathChild < minPathSum? minPathChild : minPathSum;
  34. }
  35.  
  36. return cost + minPathSum;
  37. }
  38.  
  39. static void Main(string[] args)
  40. {
  41.  
  42. }
  43. }
  44. /*
  45. recursive depth first search
  46.  
  47. if node is null
  48. return 0
  49.  
  50. if node has no children
  51. return node.value
  52.  
  53. minpath
  54. foreach(child)
  55. {
  56. keep min path
  57. }
  58.  
  59. return node.value + minPathOfallChildren
  60. */
Add Comment
Please, Sign In to add comment