Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package binaryTree;
- class Node {
- Integer data;
- Node left;
- Node right;
- public Node(Integer data, Node left, Node right) {
- this.data = data;
- this.left = left;
- this.right = right;
- }
- public Node(Integer data) {
- this(data, null, null);
- }
- }
- public class BinaryTree {
- /**
- * Finds the heaviest path in the tree from the given @root Node to a leaf Node.
- * @param root
- * @return integer representing the sum weight of the heaviest path.
- */
- public static int getHeaviestPathWeight(Node root) {
- if (root == null)
- return 0;
- else
- return root.data + Math.max(getHeaviestPathWeight(root.left), getHeaviestPathWeight(root.right));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement