/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return TreeNode类 */ bool ismid(struct TreeNode* root ){ // 判断是否是叶节点父节点 if(root == NULL){ return false; } if(root->left == NULL && root->right == NULL){ return false; } if(root->left != NULL){ if(root->left->left ==NULL && root->left->right == NULL){ return true; } } if(root->right != NULL){ if(root->right->left ==NULL && root->right->right == NULL){ return true; } } return false; } struct TreeNode* pruneLeaves(struct TreeNode* root ) { if(ismid(root)){ root = NULL; } else{ if(root->left != NULL){ root->left = pruneLeaves(root->left); } if(root->right != NULL){ root->right = pruneLeaves(root->right); } } return root; }