Problem Description
Write a function to calculate the sum of all paths in a given binary tree.
A path is defined as the route from the root to a leaf node, and the sum of this path is the sum of the values of all nodes included in it.
Input Format
- Input: The root node of the binary tree is an instance of the TreeNode class.
Output Format
- Output: Returns a list containing the sum of all paths.
Example
Input: [1, 2, 3] Output: [3, 4]
In this example, the paths are 1 -> 2 and 1 -> 3. The sum of the first path is 3, and the sum of the second path is 4.
Problem Analysis
To solve this problem, we need a method to traverse the binary tree.
The typical algorithms for this are Depth First Search (DFS) and Breadth First Search (BFS).
In this case, DFS is more suitable. We will explore the nodes of each path using DFS and calculate the total sum of the path.
Solution Strategy
1. Visit each node using a recursive function.
2. Add the value of the current node to the path sum.
3. Check if the current node is a leaf node.
4. If it is a leaf node, add the current sum to the list.
5. If there are child nodes, recursively explore the child nodes.
6. After exploration, remove the value of the current node from the path sum to return to the parent node.
C# Code Implementation
Below is the C# code to calculate the sum of all paths in a binary tree.
using System;
using System.Collections.Generic;
public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
this.val = val;
this.left = left;
this.right = right;
}
}
public class Solution {
public IList PathSum(TreeNode root) {
List result = new List();
FindPaths(root, 0, result);
return result;
}
private void FindPaths(TreeNode node, int currentSum, List result) {
if (node == null) {
return;
}
currentSum += node.val;
// Check if we are at a leaf node
if (node.left == null && node.right == null) {
result.Add(currentSum);
} else {
// Traverse the left and right subtree
FindPaths(node.left, currentSum, result);
FindPaths(node.right, currentSum, result);
}
}
}
Code Explanation
TreeNode Class: A class that represents each node of a binary tree.
Each node can have an integer value (val), a left child (left), and a right child (right).
Solution Class: A class that contains methods for calculating path sums in a binary tree.
The PathSum
method invokes the FindPaths
method to recursively explore the nodes.
- PathSum Method: Takes the given binary tree as input and returns a list of sums for all paths.
- FindPaths Method: Takes the current node and the current path sum as arguments to explore recursively.
When reaching a leaf node, it adds the current path sum to the list.
Complexity Analysis
The time complexity of this algorithm is O(N). N is the number of nodes in the tree.
This is because we visit each node once. The space complexity is also O(H), where H is the height of the tree.
This considers the stack space used by recursive calls.
Conclusion
This problem helps in understanding how to explore binary trees and the recursive approach.
It is important to master these techniques to solve various binary tree problems.
With adequate practice, one should become familiar with binary trees and their applications.