Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. package binaryTree;
  2.  
  3. class Node {
  4. Integer data;
  5. Node left;
  6. Node right;
  7.  
  8. public Node(Integer data, Node left, Node right) {
  9. this.data = data;
  10. this.left = left;
  11. this.right = right;
  12. }
  13.  
  14. public Node(Integer data) {
  15. this(data, null, null);
  16. }
  17.  
  18. }
  19.  
  20. public class BinaryTree {
  21.  
  22. /**
  23. * Finds the heaviest path in the tree from the given @root Node to a leaf Node.
  24. * @param root
  25. * @return integer representing the sum weight of the heaviest path.
  26. */
  27. public static int getHeaviestPathWeight(Node root) {
  28. if (root == null)
  29. return 0;
  30. else
  31. return root.data + Math.max(getHeaviestPathWeight(root.left), getHeaviestPathWeight(root.right));
  32. }
  33.  
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement