问题:
1.为什么能从尾到头进行删除呢?因为是后续遍历。
2.不能delete root节点,否则会由于问题。
正文:
class Solution {
public:
TreeNode* pruneTree(TreeNode* root) {
if(!root) return nullptr;
root->left = pruneTree(root->left);
root->right = pruneTree(root->right);
if(root->val == 0 && root->left == nullptr
&& root->right == nullptr){
// 不能把根节点给删除了
// delete root;
return nullptr;
} else{
return root;
}
}
};
网友评论