-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
1325. Delete Leaves With a Given Value #268
Comments
The problem asks us to delete all leaf nodes in a binary tree with a specified value. If, after deleting a leaf node, its parent node becomes a leaf with the same value, we must continue deleting such nodes recursively until no more can be deleted. Key Points
Approach
Plan
Let's implement this solution in PHP: 1325. Delete Leaves With a Given Value <?php
/**
* @param TreeNode $root
* @param Integer $target
* @return TreeNode
*/
function removeLeafNodes($root, $target) {
if ($root == null) return null;
if ($root->left == null && $root->right == null && $root->val == $target)
return null;
$root->left = $this->removeLeafNodes($root->left, $target);
$root->right = $this->removeLeafNodes($root->right, $target);
if ($root->left == null && $root->right == null && $root->val == $target)
return null;
return $root;
}
// Example usage:
$root = [1,2,3,2,null,2,4];
$target = 2;
echo removeLeafNodes($root, $target) . "\n"; // Output: [1,null,3,null,4]
$root = [1,3,3,3,2];
$target = 3;
echo removeLeafNodes($root, $target) . "\n"; // Output: [1,3,null,null,2]
$root = [1,2,null,2,null,2];
$target = 2;
echo removeLeafNodes($root, $target) . "\n"; // Output: [1]
?> Explanation:The solution works as follows:
Example WalkthroughConsider the first example: Input: The tree is:
After the removal, the root has no more leaf nodes with value 2, and the tree is returned with the new structure. Time Complexity
Output for Example
This approach efficiently solves the problem using recursion to process the tree nodes in a depth-first manner. It handles the deletion of leaf nodes with the target value and ensures that all affected parent nodes are checked for potential deletions. This solution works within the problem's constraints and provides an optimized solution with a time complexity of O(N). |
…submissions 1260759989 Co-authored-by: kovatz <[email protected]> Co-authored-by: topugit <[email protected]> Co-authored-by: basharul-siddike <[email protected]> Co-authored-by: hafijul233 <[email protected]>
…submissions 1260759989 Co-authored-by: kovatz <[email protected]> Co-authored-by: topugit <[email protected]> Co-authored-by: basharul-siddike <[email protected]> Co-authored-by: hafijul233 <[email protected]>
…submissions 1260759989 Co-authored-by: kovatz <[email protected]> Co-authored-by: topugit <[email protected]> Co-authored-by: basharul-siddike <[email protected]> Co-authored-by: hafijul233 <[email protected]>
Discussed in #267
Originally posted by mah-shamim August 9, 2024
Topics:
Tree
,Depth-First Search
,Binary Tree
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:
After removing, new nodes become leaf nodes with value (target = 2) (Picture in center).
Example 2:
Example 3:
Constraints:
[1, 3000]
.1 <= Node.val, target <= 1000
Hint:
The text was updated successfully, but these errors were encountered: