-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path#146.cpp
More file actions
71 lines (61 loc) · 1.6 KB
/
#146.cpp
File metadata and controls
71 lines (61 loc) · 1.6 KB
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
67
68
69
70
71
#include<iostream>
#include<string>
using namespace std;
struct Node {
int val;
Node* left;
Node* right;
Node(int v) {
val = v;
left = nullptr;
right = nullptr;
}
};
void printBT(string prefix, Node* node, bool isLeft) {
if (node != nullptr) {
cout << prefix << (isLeft ? "├──" : "└──" ) << node->val << endl;
printBT(prefix + (isLeft ? "│ " : " "), node->left, true);
printBT(prefix + (isLeft ? "│ " : " "), node->right, false);
}
}
bool recurse(Node* curr) {
// Base case: nullptr is a zero tree
if (curr == nullptr) return true;
// Check if left and right subtrees are zero or nullptr
// If so, subtree can be pruned
bool left_is_zero = recurse(curr->left);
bool right_is_zero = recurse(curr->right);
if (left_is_zero) curr->left = nullptr;
if (right_is_zero) curr->right = nullptr;
// If left and right subtrees are zero and curr is zero,
// then the current subtree can be pruned.
if (left_is_zero && right_is_zero && curr->val == 0) return true;
else return false;
}
Node* prune_zeros(Node* root) {
// recursively prune tree
bool all_zero = recurse(root);
// return nullptr if entire tree is zero
if (all_zero)
return nullptr;
else
return root;
}
int main() {
Node* n1 = new Node(0);
Node* n2 = new Node(1);
Node* n3 = new Node(0);
Node* n4 = new Node(1);
Node* n5 = new Node(0);
Node* n6 = new Node(0);
Node* n7 = new Node(0);
n1->left = n2;
n1->right = n3;
n3->left = n4;
n3->right = n5;
n4->left = n6;
n4->right = n7;
printBT("", n1, false);
Node* pruned_tree = prune_zeros(n1);
printBT("", pruned_tree, false);
}