Created
April 19, 2018 13:51
-
-
Save s4553711/4ca367c1e0045b5bf388b0cfe72d4b14 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* 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