LeetCode 1448. Count Good Nodes in Binary Tree
原题链接
中等
作者:
JasonSun
,
2020-08-28 02:11:03
,
所有人可见
,
阅读 449
Tree Fold
class Solution {
public:
int goodNodes(TreeNode* root) {
const auto solution = [&](int acc = 0) {
std::function<void(TreeNode*, int)> fold = [&](TreeNode* n, int subacc_max) {
if (n == nullptr) {
return;
}
else {
if (n->val >= subacc_max) {
++acc;
}
fold(n->left, std::max(subacc_max, n->val));
fold(n->right, std::max(subacc_max, n->val));
}
};
return fold(root, INT_MIN), acc;
}();
return solution;
}
};