Given a Binary Tree, the task is to print all the root to leaf path sum of the given Binary Tree.
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).
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:
- Start from the root node of the Binary tree with the initial path sum of 0.
- Add the value of the current node to the path sum.
- Travel to the left and right child of the current node with the present value of the path sum.
- Repeat Step 2 and 3 for all the subsequent nodes of the binary tree.
- On reaching the leaf node, add the path sum to the vector of pathSum.
- Print all the elements of the vector pathSum as the output.
Below is the implementation of the above approach:
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.
- Find if there is a pair in root to a leaf path with sum equals to root's data
- Find the maximum sum leaf to root path in a Binary Tree
- Print the longest leaf to leaf path in a Binary tree
- Print the first shortest root to leaf path in a Binary Tree
- Boundary Root to Leaf Path traversal of a Binary Tree
- Print the longest path from root to leaf in a Binary tree
- Maximize count of set bits in a root to leaf path in a binary tree
- GCD from root to leaf path in an N-ary tree
- Given a binary tree, print out all of its root-to-leaf paths one per line.
- Given a binary tree, print all root-to-leaf paths
- Find maximum GCD value from root to leaf in a Binary tree
- Root to leaf path sum equal to a given number
- Sum of nodes on the longest path from root to leaf node
- Shortest root to leaf path sum equal to a given number
- Root to leaf paths having equal lengths in a Binary Tree
- Count the number of paths from root to leaf of a Binary tree with given XOR value
- Count of root to leaf paths whose permutation is palindrome in a Binary Tree
- Maximum value of Bitwise AND from root to leaf in a Binary tree
- Count of root to leaf paths in a Binary Tree that form an AP
- Print path from root to all nodes in a Complete Binary Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.