Skip to content

Instantly share code, notes, and snippets.

@s4553711
Created April 19, 2018 13:51
Show Gist options
  • Save s4553711/4ca367c1e0045b5bf388b0cfe72d4b14 to your computer and use it in GitHub Desktop.
Save s4553711/4ca367c1e0045b5bf388b0cfe72d4b14 to your computer and use it in GitHub Desktop.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* pruneTree(TreeNode* root) {
if (!root) return root;
root->left = pruneTree(root->left);
root->right = pruneTree(root->right);
if (root->val == 1 || root->left || root->right)
return root;
delete root;
return nullptr;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment