Skip to content

Commit bf2ba72

Browse files
committedApr 10, 2025
Add solution #1325
1 parent 79bd183 commit bf2ba72

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,230 LeetCode solutions in JavaScript
1+
# 1,231 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1004,6 +1004,7 @@
10041004
1320|[Minimum Distance to Type a Word Using Two Fingers](./solutions/1320-minimum-distance-to-type-a-word-using-two-fingers.js)|Hard|
10051005
1323|[Maximum 69 Number](./solutions/1323-maximum-69-number.js)|Easy|
10061006
1324|[Print Words Vertically](./solutions/1324-print-words-vertically.js)|Medium|
1007+
1325|[Delete Leaves With a Given Value](./solutions/1325-delete-leaves-with-a-given-value.js)|Medium|
10071008
1331|[Rank Transform of an Array](./solutions/1331-rank-transform-of-an-array.js)|Easy|
10081009
1332|[Remove Palindromic Subsequences](./solutions/1332-remove-palindromic-subsequences.js)|Easy|
10091010
1333|[Filter Restaurants by Vegan-Friendly, Price and Distance](./solutions/1333-filter-restaurants-by-vegan-friendly-price-and-distance.js)|Medium|
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* 1325. Delete Leaves With a Given Value
3+
* https://leetcode.com/problems/delete-leaves-with-a-given-value/
4+
* Difficulty: Medium
5+
*
6+
* Given a binary tree root and an integer target, delete all the leaf nodes with value target.
7+
*
8+
* Note that once you delete a leaf node with value target, if its parent node becomes a leaf
9+
* node and has the value target, it should also be deleted (you need to continue doing that
10+
* until you cannot).
11+
*/
12+
13+
/**
14+
* Definition for a binary tree node.
15+
* function TreeNode(val, left, right) {
16+
* this.val = (val===undefined ? 0 : val)
17+
* this.left = (left===undefined ? null : left)
18+
* this.right = (right===undefined ? null : right)
19+
* }
20+
*/
21+
/**
22+
* @param {TreeNode} root
23+
* @param {number} target
24+
* @return {TreeNode}
25+
*/
26+
var removeLeafNodes = function(root, target) {
27+
return pruneLeaves(root);
28+
29+
function pruneLeaves(node) {
30+
if (!node) return null;
31+
32+
node.left = pruneLeaves(node.left);
33+
node.right = pruneLeaves(node.right);
34+
35+
if (!node.left && !node.right && node.val === target) {
36+
return null;
37+
}
38+
39+
return node;
40+
}
41+
};

0 commit comments

Comments
 (0)
Please sign in to comment.