1.递归操作很简单,就是当root为0且左右节点都为空时删除节点
2.细节:如果不是c++不能使用delete
class Solution {
public:
TreeNode* pruneTree(TreeNode* root) {
if(root==nullptr)
{
return nullptr;
}
root->left=pruneTree(root->left);
root->right=pruneTree(root->right);
if(root->left==nullptr&&root->right==nullptr&&root->val==0)
{
delete root;
root=nullptr;
return root;
}
return root;
}
};