一、题目
We are given the head node root of a binary tree, where additionally every node’s value is either a 0 or a 1.
Return the same tree where every subtree (of the given tree) not containing a 1 has been removed.
(Recall that the subtree of a node X is X, plus every node that is a descendant of X.)
二、题目大意
给定一个二叉树,如果二叉树的子树中不包含1则删除,返回新的子树。
三、解题思路
采用递归的方法,在递归的过程中,需要判断当前子树是保留还是删除:
(root.val == 1 || root.left != null || root.right != null) ? root : null
四、代码实现
const pruneTree = root => {
if (!root) {
return null
}
root.left = pruneTree(root.left)
root.right = pruneTree(root.right)
return (root.val == 1 || root.left != null || root.right != null) ? root : null
}
如果本文对您有帮助,欢迎关注微信公众号,为您推送更多内容,ε=ε=ε=┏(゜ロ゜;)┛。