Created
May 18, 2024 09:39
-
-
Save shitu13/b0ab601b39594c3f0ac53ffca66649a7 to your computer and use it in GitHub Desktop.
Delete Leaves With a Given Value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
TreeNode* removeLeafNodes(TreeNode* root, int target) { | |
if (root == NULL) | |
return NULL; | |
root->left = removeLeafNodes(root->left, target); | |
root->right = removeLeafNodes(root->right, target); | |
if (root->left == NULL && root->right == NULL && root->val == target) | |
return NULL; | |
return root; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment