The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as
well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. Donald E. Knuth


Problem :-
Print the sum of all values in the ancestral nodes of a given node in a Binary Tree.
Following figure shows a Binary Tree and the another tree with node values replaced by the ancestral sum. For e.g, for the node 1 , sum of the values of ancestral nodes is 1 + 3 + 7 = 11 and for the node 4 , sum of ancestral nodes is 4 + 5 + 3 + 7 = 19 .

ancestral node sum

Solution :-
This problem can be solved by a minor extension of any of the traversal mechanism. We can do an inorder traversal of the tree and maintain the sum of the ancestral node values. See the implementation below.

using namespace std;

typedef struct tree_node {
   int value;
   struct tree_node *left, *right;

// 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;

// create the tree
node *createTree() {
   node *root = getNewNode(7);
   root->left = getNewNode(3);
   root->right = getNewNode(11);
   root->left->left = getNewNode(1);
   root->left->right = getNewNode(5);
   root->left->right->left = getNewNode(4);
   root->left->right->right = getNewNode(6);
   return root;

// Inorder traversal of a tree
void inorderTraversal(node *ptr) {
   if(ptr == NULL)
   else {

// Print the sum of all values in the ancestral nodes of a given node
void ancestralSums(node *root,int sum) {
   if (!root) return;
   sum += root->value;

// main
int main() {
   node *root = createTree();
   cout<<"\nInorder traversal ...\n\n";
   cout<<"\n\nAncestral Sum :-\n\n";
   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 )