Tree Sum
Question
Given a root of a tree, return the total sum of all values in the binary tree.
input:
graph TD;
1-->3;
1-->2;
2-->6;
2-->7;
3-->4;
3-->8;
Output:
Solution
we can solve this by traversing through the graph and adding all our values as we go.
This can be done either through DFS (pre,post,in) or BFS.
Javascript
const treeSum = (root) => {
if (root === null) return 0;
return root.val + treeSum(root.left) + treeSum(root.right);
};
Java
Concepts
Patterns
- DFS