8/23/2016 google 面经

给你一个多叉树。移出所有子树,当这个子树的全部节点的值的和为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;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容