forked from Nimesh-Srivastava/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0814.cpp
More file actions
30 lines (26 loc) · 674 Bytes
/
0814.cpp
File metadata and controls
30 lines (26 loc) · 674 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
26
27
28
29
30
// Solution 1 :-
class Solution {
public:
TreeNode *pruneTree(TreeNode *root) {
if (!root)
return nullptr;
root->left = pruneTree(root->left);
root->right = pruneTree(root->right);
if (root->val == 1 or root->left or root->right)
return root;
return nullptr;
}
};
// Solution 2 :-
class Solution {
public:
TreeNode* pruneTree(TreeNode* root) {
if (!root)
return NULL;
root -> left = pruneTree(root -> left);
root -> right = pruneTree(root -> right);
if (!root -> left && !root -> right && root -> val == 0)
return NULL;
return root;
}
};