10.删除值为target的叶子结点

class Solution {
public:
    TreeNode* dfs(TreeNode *&root,int target){
        if(!root) return root;
        dfs(root->left,target);
        dfs(root->right,target);
        if(root->val==target&&!root->left&&!root->right){
            root=NULL;
        }
        return root;
    }
    TreeNode* removeLeafNodes(TreeNode* root, int target) {
        return dfs(root,target);   
    }
};

  直接令其为空即可

posted @ 2020-07-10 15:23  阿破  阅读(106)  评论(0编辑  收藏  举报