-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDelete Leaves With a Given Value.cpp
37 lines (33 loc) · 1.1 KB
/
Delete Leaves With a Given Value.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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
#pragma GCC optimize("O3", "unroll-loops")
class Solution {
public:
static TreeNode* removeLeafNodes(TreeNode* root, int target, TreeNode* parent=NULL) {
if (!root) return NULL;
// Recursively remove leaf nodes from left and right subtrees
root->left = removeLeafNodes(root->left, target, root);
root->right = removeLeafNodes(root->right, target, root);
// If current node is a leaf(not root) and its value==target, delete it
if (!root->left && !root->right && root->val==target) {
if (parent) delete root; //the root cannot be deleted
return NULL;
}
return root;
}
};
auto init = []() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
return 'c';
}();