Skip to content

Commit 70b6e54

Browse files
authored
Create 1325. Delete Leaves With a Given Value (#481)
2 parents cb005e7 + 47fd70a commit 70b6e54

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
TreeNode* removeLeafNodes(TreeNode* root, int t) {
15+
if(root->right)root->right = removeLeafNodes(root->right, t);
16+
if(root->left)root->left = removeLeafNodes(root->left, t);
17+
if(!root->left && !root->right && root->val == t) return NULL;
18+
return root;
19+
}
20+
};

0 commit comments

Comments
 (0)