美文网首页
Binary Tree Pruning 小结 (Leetcode

Binary Tree Pruning 小结 (Leetcode

作者: stepsma | 来源:发表于2020-04-01 00:55 被阅读0次

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

      本文链接:https://www.haomeiwen.com/subject/tlinuhtx.html