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); } };
直接令其为空即可