-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
sum_root_to_leaf.cc
30 lines (25 loc) · 1023 Bytes
/
sum_root_to_leaf.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "binary_tree_node.h"
#include "test_framework/generic_test.h"
int SumRootToLeafHelper(const unique_ptr<BinaryTreeNode<int>>&, int);
int SumRootToLeaf(const unique_ptr<BinaryTreeNode<int>>& tree) {
return SumRootToLeafHelper(tree, /*partial_path_sum=*/0);
}
int SumRootToLeafHelper(const unique_ptr<BinaryTreeNode<int>>& tree,
int partial_path_sum) {
if (tree == nullptr) {
return 0;
}
partial_path_sum = partial_path_sum * 2 + tree->data;
if (tree->left == nullptr && tree->right == nullptr) { // Leaf.
return partial_path_sum;
}
// Non-leaf.
return SumRootToLeafHelper(tree->left, partial_path_sum) +
SumRootToLeafHelper(tree->right, partial_path_sum);
}
int main(int argc, char* argv[]) {
std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"tree"};
return GenericTestMain(args, "sum_root_to_leaf.cc", "sum_root_to_leaf.tsv",
&SumRootToLeaf, DefaultComparator{}, param_names);
}