Leetcode 832 Solution
This article provides solution to leetcode question 832 (binary-tree-pruning)
Access this page by simply typing in "lcs 832" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/binary-tree-pruning
Solution
/**
* 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:
bool _pruneTree(TreeNode* root) {
if (root == NULL)
return true;
if (!_pruneTree(root->left))
root->left = NULL;
if (!_pruneTree(root->right))
root->right = NULL;
if (root->val == 0 && !root->left && !root->right)
return false;
return true;
}
TreeNode* pruneTree(TreeNode* root) {
return _pruneTree(root) ? root : NULL;
}
};