Skip to content

Commit 7718f95

Browse files
committed
solved leetcode daily challenge, Binary Tree Pruning
1 parent aca1d36 commit 7718f95

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

LeetCode/Binary_Tree_Pruning/main.cxx

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <bits/stdc++.h>
2+
#include <gtest/gtest.h>
3+
using namespace std;
4+
5+
6+
//// START
7+
/*
8+
## Binary Tree Pruning
9+
10+
*/
11+
12+
13+
/**
14+
* Definition for a binary tree node.
15+
*/
16+
struct TreeNode {
17+
int val;
18+
TreeNode *left;
19+
TreeNode *right;
20+
TreeNode() : val(0), left(nullptr), right(nullptr) {}
21+
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
22+
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
23+
};
24+
25+
class Solution {
26+
public:
27+
TreeNode *pruneTree(TreeNode *root) {
28+
if (!root) return nullptr;
29+
if (!root->left && !root->right) {
30+
return root->val == 0 ? nullptr : root;
31+
}
32+
root->left = pruneTree(root->left);
33+
root->right = pruneTree(root->right);
34+
if (!root->left && !root->right && root->val == 0) return nullptr;
35+
return root;
36+
}
37+
};
38+
39+
//// END
40+
struct T {
41+
42+
};
43+
44+
TEST(Solution, test) {
45+
T ts[] = {
46+
{
47+
48+
},
49+
{
50+
51+
},
52+
53+
};
54+
55+
for (T t : ts) {
56+
Solution solution;
57+
58+
}
59+
}
60+
61+
int main() {
62+
testing::InitGoogleTest();
63+
64+
return RUN_ALL_TESTS();
65+
}
66+
67+

0 commit comments

Comments
 (0)