-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path814.cpp
More file actions
25 lines (23 loc) · 684 Bytes
/
Copy path814.cpp
File metadata and controls
25 lines (23 loc) · 684 Bytes
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
class Solution {
public:
TreeNode *pruneTree(TreeNode *root) {
if(!root || !TraverseFor1(root))
return nullptr;
return root;
}
bool TraverseFor1(TreeNode *root) {
if (!root)
return false;
bool &&leftContains1 = TraverseFor1(root->left);
if (root->left && !leftContains1) {
delete root->left;
root->left = nullptr;
}
bool &&rightContains1 = TraverseFor1(root->right);
if (root->right && !rightContains1) {
delete root->right;
root->right = nullptr;
}
return leftContains1 || rightContains1 || (root->val == 1);
}
};