From 47fd70a4a0a55be366f16ab949b1f43fa2e2c66c Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Fri, 17 May 2024 16:44:32 +0530 Subject: [PATCH] Create 1325. Delete Leaves With a Given Value --- 1325. Delete Leaves With a Given Value | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 1325. Delete Leaves With a Given Value diff --git a/1325. Delete Leaves With a Given Value b/1325. Delete Leaves With a Given Value new file mode 100644 index 0000000..bb324de --- /dev/null +++ b/1325. Delete Leaves With a Given Value @@ -0,0 +1,20 @@ +/** + * 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) {} + * }; + */ +class Solution { +public: + TreeNode* removeLeafNodes(TreeNode* root, int t) { + if(root->right)root->right = removeLeafNodes(root->right, t); + if(root->left)root->left = removeLeafNodes(root->left, t); + if(!root->left && !root->right && root->val == t) return NULL; + return root; + } +};