LeetCode 1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree
原题链接
中等
作者:
JasonSun
,
2020-08-27 13:28:45
,
所有人可见
,
阅读 483
Tree Fold
class Solution {
public:
TreeNode* getTargetCopy(TreeNode* original, TreeNode* cloned, TreeNode* target) {
const auto solution = [&](TreeNode* self = nullptr) {
std::function<void(TreeNode*, TreeNode*)> fold = [&](TreeNode* n, TreeNode* m) {
if (n == nullptr or self != nullptr) {
return;
}
else {
if (n == target) {
std::exchange(self, m);
}
fold(n->left, m->left);
fold(n->right, m->right);
}
};
return fold(original, cloned), self;
}();
return solution;
}
};