1325. Delete Leaves With a Given Value LeetCode Solution

In this guide, you will get 1325. Delete Leaves With a Given Value LeetCode Solution with the best time and space complexity. The solution to Delete Leaves With a Given Value problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Delete Leaves With a Given Value solution in C++
  4. Delete Leaves With a Given Value solution in Java
  5. Delete Leaves With a Given Value solution in Python
  6. Additional Resources
1325. Delete Leaves With a Given Value LeetCode Solution image

Problem Statement of Delete Leaves With a Given Value

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).

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.

Constraints:

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(h)
See also  806. Number of Lines To Write String LeetCode Solution

1325. Delete Leaves With a Given Value LeetCode Solution in C++

class Solution {
 public:
  TreeNode* removeLeafNodes(TreeNode* root, int target) {
    if (root == nullptr)
      return nullptr;
    root->left = removeLeafNodes(root->left, target);
    root->right = removeLeafNodes(root->right, target);
    return isLeaf(root) && root->val == target ? nullptr : root;
  }

 private:
  bool isLeaf(TreeNode* root) {
    return root->left == nullptr && root->right == nullptr;
  }
};
/* code provided by PROGIEZ */

1325. Delete Leaves With a Given Value LeetCode Solution in Java

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);
    return isLeaf(root) && root.val == target ? null : root;
  }

  private boolean isLeaf(TreeNode root) {
    return root.left == null && root.right == null;
  }
}
// code provided by PROGIEZ

1325. Delete Leaves With a Given Value LeetCode Solution in Python

class Solution:
  def removeLeafNodes(
      self,
      root: TreeNode | None,
      target: int,
  ) -> TreeNode | None:
    if not root:
      return None
    root.left = self.removeLeafNodes(root.left, target)
    root.right = self.removeLeafNodes(root.right, target)
    return None if self._isLeaf(root) and root.val == target else root

  def _isLeaf(self, root: TreeNode | None) -> bool:
    return not root.left and not root.right
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.