遇到问题:delete使用,需要查资料

    1. /**
    2. * Definition for a binary tree node.
    3. * struct TreeNode {
    4. * int val;
    5. * TreeNode *left;
    6. * TreeNode *right;
    7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
    8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
    10. * };
    11. */
    12. class Solution {
    13. public:
    14. TreeNode* pruneTree(TreeNode* root) {
    15. if(root==NULL){
    16. return root;
    17. }
    18. if(!check(root)){
    19. root=NULL;
    20. }
    21. if(root!=NULL){
    22. TreeNode* left1=pruneTree(root->left);
    23. TreeNode* right1=pruneTree(root->right);
    24. root->left=left1;
    25. root->right=right1;
    26. }
    27. return root;
    28. }
    29. bool check(TreeNode* root){
    30. if(root==NULL){
    31. return false;
    32. }
    33. if(root->val==1){
    34. return true;
    35. }
    36. bool a1=check(root->left);
    37. bool a2=check(root->right);
    38. return a1||a2;
    39. }
    40. };