-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem_8.cpp
66 lines (56 loc) · 1.54 KB
/
problem_8.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// A unival tree (which stands for "universal value") is a tree where all nodes under it have the same value.
// Given the root to a binary tree, count the number of unival subtrees.
// For example, the following tree has 5 unival subtrees:
// 0
// / \
// 1 0
// / \
// 1 0
// / \
// 1 1
// Example
// const unique_ptr<Node> node_tree (
// new Node(0,
// new Node(1),
// new Node(0,
// new Node(1,
// new Node(1),
// new Node(1)),
// new Node(0))));
//
// auto result = problem_eight(node_tree.get(), node_tree->value);
#include <memory>
using namespace std;
struct Node
{
const int value;
const Node* const child_left, *child_right;
Node(const int value, const Node* const child_left = nullptr, const Node* const child_right = nullptr)
:value(value), child_left(child_left), child_right(child_right)
{}
constexpr bool is_leaf() const
{
return this->child_left == nullptr && this->child_right == nullptr;
}
constexpr bool is_value_equal(const Node* const node) const
{
return !node || node->value == this->value;
}
~Node()
{
delete this->child_left;
delete this->child_right;
}
};
int problem_eight(const Node* const node, int value)
{
if (node == nullptr)
return 0;
if (node->is_leaf())
return 1;
const auto leaf_count = problem_eight(node->child_left, node->value) +
problem_eight(node->child_right, node->value);
return node->is_value_equal(node->child_left) && node->is_value_equal(node->child_right)
? leaf_count + 1
: leaf_count;
}