LeetCode 230. Kth Smallest Element in a BST
原题链接
中等
作者:
JasonSun
,
2020-08-16 15:32:59
,
所有人可见
,
阅读 344
Tree Fold
class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
const auto solution = [&, time_stamp = 0](std::optional<int> self = {}) mutable {
std::function<void(TreeNode*)> fold = [&](TreeNode* node) {
if (self.has_value() or node == nullptr) {
return;
}
else if (node->left == nullptr and node->right == nullptr) {
if (++time_stamp == k) {
self.emplace(node->val);
}
}
else {
fold(node->left);
if (++time_stamp == k) {
self.emplace(node->val);
}
fold(node->right);
}
};
return fold(root), self;
}();
return solution.value();
}
};