Tree

Problem 
Get the root to leaf path in a Binary Tree such that the sum of the node values in that path is minimum among all possible root to leaf paths. For e.g, for the tree shown below, minimum sum path is 31 ( 15 + 10 + 6 ) .

tree height

Solution 
We will recursively compute the minimum sum path of left and right subtrees of each node ( X ) and the minimum value obtained is added to X -> data. See the implementation below.

#include<iostream>
using namespace std;

// node structure
typedef struct tree_node {
   int value;
   struct tree_node *left, *right;
}node;

// create a new node
node *getNewNode(int value) {
   node *new_node = new node;
   new_node->value = value;
   new_node->left = NULL;
   new_node->right = NULL;
   return new_node;
}

// function to compute the minimum sum path
// It only returns the sum of the values of nodes on the min sum path 
int minSumPath(node *ptr) {
    if(ptr == NULL)
        return 0;

    int sum = ptr->value;

    int left_sum = minSumPath(ptr->left);
    int right_sum = minSumPath(ptr->right);

    if(left_sum <= right_sum)
        sum += minSumPath(ptr->left);
    else
        sum += minSumPath(ptr->right);

    return sum;
}

// create the tree
node *createTree() {
   node *root = getNewNode(15);
   root->left = getNewNode(10);
   root->right = getNewNode(20);
   root->right->left = getNewNode(22);
   root->left->left = getNewNode(6);
   root->left->right = getNewNode(7);
   root->left->right->left = getNewNode(2);
   root->left->right->right = getNewNode(16);
   root->left->right->left->left = getNewNode(0);
   root->left->right->left->right = getNewNode(5);
   root->left->right->right->left = getNewNode(3);
   return root;
}


int main() {
   node *root = NULL;
   root = createTree();
   int sum = minSumPath(root);
   cout<<"Min Sum :: "<<sum;
   cout<<endl;
   return 0;
}

Back | Next

All problems on Trees
* Implement the inorder , preorder and postorder traversal mechanisms of a tree
* Implement an algorithm to insert a node in a Binary Search Tree ( BST )
* Implement an algorithm to find the height of a Binary Tree
* Implement an algorithm to get the level of a node in a Binary Tree assuming root node to be at level 1
* Get the root to leaf path in a Binary Tree such that the sum of the node values in that path is minimum among all possible root to leaf paths
* Print all the ancestors of a given node in a Binary Tree
* Replace all the node values with the sum of the node values of ancestral nodes in a Binary Tree
* Check if the given tree is a sum tree i.e value at each node is equal to the value of all elements in its left subtree and right subtree
* Given a Binary Tree, create another tree which is a mirror image of the given tree
* Construct a tree, given its inorder and preorder traversals
* Find the Least Common Ancestor ( LCA ) of two nodes in a Binary Search Tree
* Find the Least Common Ancestor ( LCA ) of two nodes in a Binary Tree
* Compute the Diameter of a given Binary Tree
* Check if a given tree is a Binary Search Tree ( BST )
About    Contact    Sitemap    Terms    Privacy