用时:5min
虚拟节点 + 后序遍历 就可以愉快的秒了
var removeLeafNodes = function(root, target) {
var dfs = function (root) {
if (root.left) {
root.left = dfs(root.left)
}
if (root.right) {
root.right = dfs(root.right)
}
if (!root.left && !root.right) {
if (root.val === target) {
root = null }
}
return root }
var dummy = new TreeNode(0)
dummy.left = root dfs(dummy)
return dummy.left};