Skip to content

Instantly share code, notes, and snippets.

@shitu13
Created May 18, 2024 09:39
Show Gist options
  • Save shitu13/b0ab601b39594c3f0ac53ffca66649a7 to your computer and use it in GitHub Desktop.
Save shitu13/b0ab601b39594c3f0ac53ffca66649a7 to your computer and use it in GitHub Desktop.
Delete Leaves With a Given Value
class Solution {
public:
TreeNode* removeLeafNodes(TreeNode* root, int target) {
if (root == NULL)
return NULL;
root->left = removeLeafNodes(root->left, target);
root->right = removeLeafNodes(root->right, target);
if (root->left == NULL && root->right == NULL && root->val == target)
return NULL;
return root;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment