I'm struggling to find an algorithm for the following problem:
Given a binary tree of integers, the cost of a branch (a.k.a. a branch that starts from the root and reaches the leaf node) is given by the sum of his values. Write a function that returns a list of the cheapest branch.
Can anyone recommend me the easiest way to complete this exercise?
The shortest path between any two nodes in a tree must pass through their Lowest Common Ancestor (LCA). The path will travel upwards from node s to the LCA and then downwards from the LCA to node t. Find the path strings from root → s, and root → t.
The distance between two nodes can be obtained in terms of lowest common ancestor. Following is the formula. Dist(n1, n2) = Dist(root, n1) + Dist(root, n2) - 2*Dist(root, lca) 'n1' and 'n2' are the two given keys 'root' is root of given Binary Tree.
Approach: Create a recursive function that traverses the different path in the binary tree to find the required node x. If node x is present then it returns true and accumulates the path nodes in some array arr[]. Else it returns false.
If the root node is null then no path exists, return an empty vector. Get the longest path from right subtree in a vector rightvect by recursively traversing root -> right. Similarly, get the longest path from left subtree in a vector leftvect by recursively traversing root -> left.
It can easily be done recursively . The function prints all the root to leaf paths along with the cheapest branch. I have used Arraylist to add all the nodes from root to leaf. Whenever a leaf node is reached i just check if the maxSum so far is smaller than then the current root to leaf path and update it.
class Node {
public int info;
public Node left;
public Node right;
public Node(int info) {
this(info, null, null);
}
public Node(int info, Node left, Node right) {
this.info = info;
this.left = left;
this.right = right;
}
}
public class RootToLeaf {
private static int maxSum = Integer.MAX_VALUE;
private static ArrayList<Integer> finalList = new ArrayList<>();
public static void main(String[] args) {
Node root = new Node(8);
root.left = new Node(4);
root.left.left = new Node(3);
root.left.right = new Node(1);
root.right = new Node(5);
root.right.right = new Node(11);
ArrayList<Integer> list = new ArrayList<Integer>();
path(root, list,0);
System.out.println("Cheapest list is - " + finalList.toString() + " and minimum sum is " + maxSum);
}
private static void path(Node root, ArrayList<Integer> list,int s) {
if(root==null) {
return;
} else {
list.add(root.info);
s = s+root.info;
}
if ((root.left == null && root.right == null)) {
System.out.println(list);
if(maxSum>s) {
maxSum = s;
finalList = new ArrayList<>(list);
}
return;
}
path(root.left, new ArrayList<Integer>(list),s);
path(root.right, new ArrayList<Integer>(list),s);
}
}
The out put is as follows :
[8, 4, 3]
[8, 4, 1]
[8, 5, 11]
Cheapest list is - [8, 4, 1] and minimum sum is 13
As a hint, work from the leaves of the tree upward. The cost of a leaf is just the value inside the leaf. Otherwise, the cost of a the best path starting at a node is given by the cost of that node plus the cost of the cheapest path taken from there. Can you implement this recursively?
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With