Binary Tree Pruning 小结 (Leetcode 814, 669, 1325)

在做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;
    }
};

你可能感兴趣的:(Binary Tree Pruning 小结 (Leetcode 814, 669, 1325))