LeetCode 156. Binary Tree Upside Down
原题链接
中等
作者:
JasonSun
,
2020-08-16 10:09:06
,
所有人可见
,
阅读 338
Fmap with side effects
class Solution {
public:
TreeNode *upsideDownBinaryTree(TreeNode *root) {
std::function<TreeNode*(TreeNode*, TreeNode*, TreeNode*)> fmap = [&](TreeNode* node, TreeNode* parent, TreeNode* parent_rch) {
if (node == nullptr)
return parent;
else
return fmap(std::exchange(node->left, parent_rch), node, std::exchange(node->right, parent));
};
return fmap(root, nullptr, nullptr);
}
};