-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy path1325-delete-leaves-with-a-given-value.js
More file actions
41 lines (36 loc) · 1.08 KB
/
1325-delete-leaves-with-a-given-value.js
File metadata and controls
41 lines (36 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* 1325. Delete Leaves With a Given Value
* https://leetcode.com/problems/delete-leaves-with-a-given-value/
* Difficulty: Medium
*
* 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).
*/
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} target
* @return {TreeNode}
*/
var removeLeafNodes = function(root, target) {
return pruneLeaves(root);
function pruneLeaves(node) {
if (!node) return null;
node.left = pruneLeaves(node.left);
node.right = pruneLeaves(node.right);
if (!node.left && !node.right && node.val === target) {
return null;
}
return node;
}
};