靠邀第一次用网页版发文
这个是vimㄇ==
看到^Q才想到要怎么贴上
我不太会用ㄟ
打完桌游准备要睡
又水了一题:)))
可是大家都写得好干净喔
只剩我写得这么冗了
第一个if是不需要ㄇ??
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
right(right) {}
* };
*/
class Solution {
public:
TreeNode* removeLeafNodes(TreeNode* root, int target) {
if(root->left == nullptr and root->right == nullptr){
if(root->val == target){
return nullptr;
}
}
if(root->left != nullptr){
root->left = removeLeafNodes(root->left, target);
}
if(root->right != nullptr){
root->right = removeLeafNodes(root->right, target);
}
if(root->left == nullptr and root->right == nullptr){
if(root->val == target){
return nullptr;
}
}
return root;
}
};