<p>Given a binary tree <code>root</code> and an integer <code>target</code>, delete all the <strong>leaf nodes</strong> with value <code>target</code>.</p>

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

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/01/09/sample_1_1684.png" style="width: 500px; height: 112px;" /></strong></p>

<pre>
<strong>Input:</strong> root = [1,2,3,2,null,2,4], target = 2
<strong>Output:</strong> [1,null,3,null,4]
<strong>Explanation:</strong> 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).
</pre>

<p><strong class="example">Example 2:</strong></p>

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/01/09/sample_2_1684.png" style="width: 400px; height: 154px;" /></strong></p>

<pre>
<strong>Input:</strong> root = [1,3,3,3,2], target = 3
<strong>Output:</strong> [1,3,null,null,2]
</pre>

<p><strong class="example">Example 3:</strong></p>

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/01/15/sample_3_1684.png" style="width: 500px; height: 166px;" /></strong></p>

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

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[1, 3000]</code>.</li>
	<li><code>1 &lt;= Node.val, target &lt;= 1000</code></li>
</ul>

/**
 * 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:
    bool hasTargetLeafNode(TreeNode* node, int target) {
        if (node->val == target && node->left == nullptr && node->right == nullptr)
            return false;
        bool ans = false;
        dfs(node, target, ans);
        return ans;
    }

    void dfs(TreeNode* node, int target, bool& ans) {
        if (node == nullptr)
            return;

        if (node->val == target && node->left == nullptr && node->right == nullptr) {
            ans = true;
            return;
        }

        dfs(node->left, target, ans);
        dfs(node->right, target, ans);
    }

    void dfs(TreeNode* node, int target) {
        if (node == nullptr)
            return;
        
        if (node->left != nullptr && node->left->val == target)
            if (node->left->left == nullptr && node->left->right == nullptr)
                node->left = nullptr;

        if (node->right != nullptr && node->right->val == target)
            if (node->right->left == nullptr && node->right->right == nullptr)
                node->right = nullptr;

        dfs(node->left, target);
        dfs(node->right, target);
    }

    TreeNode* removeLeafNodes(TreeNode* root, int target) {
        while (hasTargetLeafNode(root, target))
            dfs(root, target);

        if (root->val == target && root->left == nullptr && root->right == nullptr)
            return nullptr;

        return root;
    }
};
23000