作者:
sixB (6B)
2024-05-18 04:54:43靠邀第一次用網頁版發文
這個是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;
}
};