在做pruning时,需要用到以下template:
root->left = pruneTree(root->left);
root->right = pruneTree(root->right);
sample code for leetcode 814:
https://leetcode.com/problems/binary-tree-pruning/
class Solution {
public:
TreeNode* pruneTree(TreeNode* root) {
if(!root){
return NULL;
}
root->left = pruneTree(root->left);
root->right = pruneTree(root->right);
if(!root->left && !root->right && root->val == 0){
return NULL;
}
return root;
}
};