Given a binary tree root and an integer target, delete all the leaf nodes with value target.

Note that once you delete a leaf node with value target, if its parent node becomes a leaf node and has the value target, it should also be deleted (you need to continue doing that until you cannot).

&nbsp;
Example 1:



Input: root = [1,2,3,2,null,2,4], target = 2
Output: [1,null,3,null,4]
Explanation: Leaf nodes in green with value (target = 2) are removed (Picture in left). 
After removing, new nodes become leaf nodes with value (target = 2) (Picture in center).


Example 2:



Input: root = [1,3,3,3,2], target = 3
Output: [1,3,null,null,2]


Example 3:



Input: root = [1,2,null,2,null,2], target = 2
Output: [1]
Explanation: Leaf nodes in green with value (target = 2) are removed at each step.


&nbsp;
Constraints:


	The number of nodes in the tree is in the range [1, 3000].
	1 &lt;= Node.val, target &lt;= 1000


class Solution:
    def removeLeafNodes(self, root: Optional[TreeNode], target: int) -> Optional[TreeNode]:
        if not root:
            return None
        root.left = self.removeLeafNodes(root.left, target)
        root.right = self.removeLeafNodes(root.right, target)
        if not root.left and not root.right and root.val == target:
            return None
        return root
class Solution {
    public TreeNode removeLeafNodes(TreeNode root, int target) {
        if(root==null)
            return root;
        root.left = removeLeafNodes(root.left,target);
        root.right = removeLeafNodes(root.right,target);
        if(root.left == null && root.right == null && root.val == target)
            root = null;
        return root;
    }
}
/**
 * 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 target) {
        if(root==NULL || root->left == NULL && root->right == NULL && root->val == target)
            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;

    }
};
var removeLeafNodes = function(root, target) {
    const parent = new TreeNode(-1, root, null);

    const traverse = (r = root, p = parent, child = -1) => {
        if(!r) return null;
        traverse(r.left, r, -1);
        traverse(r.right, r, 1);
        if(r.left == null && r.right == null && r.val == target) {
            if(child == -1) p.left = null;
            else p.right = null;
        }
    }
    traverse();
    return parent.left;
};
Delete Leaves With a Given Value