https://leetcode.com/problems/delete-leaves-with-a-given-value/
I decided to stop solving daily problems everyday. I'm so tired. I'm going to join contests sometimes.
Today's problem is a easy tree problem.
let isLeaf = (node) => !node.left && !node.right;
let remove = (node, target) => {
if (!node) return true;
if (remove(node.left, target)) {
node.left = null;
}
if (remove(node.right, target)) {
node.right = null;
}
return isLeaf(node) && node.val === target;
};
var removeLeafNodes = function (root, target) {
return remove(root, target) ? null : root;
};