# Find all root to leaf path sum of a Binary Tree

Given a Binary Tree, the task is to print all the root to leaf path sum of the given Binary Tree.

Examples:

```Input:
30
/      \
10        50
/  \      /  \
3   16   40    60
Output: 43 56 120 140
Explanation:
In the above binary tree
there are 4 leaf nodes.
Hence, total 4 path sum are
present from root node to the
leaf node.
(i.e., 30-10-3, 30-10-16,
30-50-40, 30-50-60)
Therefore, the path sums from
left to right would be
(43, 56, 120, 140).

Input:
11
/      \
12        5
\      /
16   40
Output: 39 56
Explanation:
In the above binary tree
there are 2 leaf nodes.
Hence, total 2 path sum are
present from root node to
the leaf node.
(i.e 11-12-16, 11-5-40)
Therefore, the path sums from
left to right would be (39 56).
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to use DFS Traversal to travel from the root to the leaf of the binary tree and calculate the sum of each root to leaf path. Follow the steps below to solve the problem:

1. Start from the root node of the Binary tree with the initial path sum of 0.
2. Add the value of the current node to the path sum.
3. Travel to the left and right child of the current node with the present value of the path sum.
4. Repeat Step 2 and 3 for all the subsequent nodes of the binary tree.
5. On reaching the leaf node, add the path sum to the vector of pathSum.
6. Print all the elements of the vector pathSum as the output.

Below is the implementation of the above approach:

## C++14

 `// C++ program for the above approach ` `#include ` `using` `namespace` `std; ` ` `  `// TreeNode structure ` `struct` `TreeNode { ` `    ``int` `val; ` `    ``TreeNode *left, *right; ` `}; ` ` `  `// Function that returns a new TreeNode ` `// with given value ` `struct` `TreeNode* addNode(``int` `data) ` `{ ` `    ``// Allocate memory ` `    ``struct` `TreeNode* node ` `        ``= (``struct` `TreeNode*)``malloc``( ` `            ``sizeof``(``struct` `TreeNode)); ` ` `  `    ``// Assign data to val variable ` `    ``node->val = data; ` `    ``node->left = NULL; ` `    ``node->right = NULL; ` `    ``return` `node; ` `}; ` ` `  `// Function that calculates the root to ` `// leaf path sum of the BT using DFS ` `void` `dfs(TreeNode* root, ``int` `sum, ` `         ``vector<``int``>& pathSum) ` `{ ` `    ``// Return if the node is NULL ` `    ``if` `(root == NULL) ` `        ``return``; ` ` `  `    ``// Add value of the node to ` `    ``// the path sum ` `    ``sum += root->val; ` ` `  `    ``// Store the path sum if node is leaf ` `    ``if` `(root->left == NULL ` `        ``and root->right == NULL) { ` ` `  `        ``pathSum.push_back(sum); ` `        ``return``; ` `    ``} ` ` `  `    ``// Move to the left child ` `    ``dfs(root->left, sum, pathSum); ` ` `  `    ``// Move to the right child ` `    ``dfs(root->right, sum, pathSum); ` `} ` ` `  `// Function that finds the root to leaf ` `// path sum of the given binary tree ` `void` `findPathSum(TreeNode* root) ` `{ ` `    ``// To store all the path sum ` `    ``vector<``int``> pathSum; ` ` `  `    ``// Calling dfs function ` `    ``dfs(root, 0, pathSum); ` ` `  `    ``// Printing all the path sum ` `    ``for` `(``int` `num : pathSum) { ` `        ``cout << num << ``" "``; ` `    ``} ` `    ``cout << endl; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``// Given binary tree ` `    ``TreeNode* root = addNode(30); ` `    ``root->left = addNode(10); ` `    ``root->right = addNode(50); ` `    ``root->left->left = addNode(3); ` `    ``root->left->right = addNode(16); ` `    ``root->right->left = addNode(40); ` `    ``root->right->right = addNode(60); ` ` `  `    ``/*  The above code constructs this tree ` ` `  `                ``30 ` `             ``/      \ ` `           ``10        50 ` `          ``/  \      /  \ ` `         ``3   16   40    60 ` `   ``*/` ` `  `    ``// Function Call ` `    ``findPathSum(root); ` ` `  `    ``return` `0; ` `} `

Output:

```43 56 120 140
```

Time Complexity: O(N)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.