给你一个多叉树。移出所有子树,当这个子树的全部节点的值的和为0。
struct TreeNode {
vector children;
int val;
TreeNode(int v) : val(v) {};
~TreeNode() { for (TreeNode* cnode: children) delete cnode; }
};
int subtree_sum(TreeNode* &pnode) {
if (pnode == nullptr) return 0;. visit 1point3acres.com for more.
int sum = pnode->val;
for (TreeNode* cnode : pnode->children) sum += subtree_sum(cnode);
if (sum == 0) {
delete pnode;-google 1point3acres
pnode = NULL;.
}
return sum;
}