剑指 Offer 54. 二叉搜索树的第k大节点 - 力扣(LeetCode) (leetcode-cn.com)
其实就是中序遍历
class Solution {
public:
int kthLargest(TreeNode* root, int k) {
int count = 0;
stack<TreeNode*> nodeStack;
while (root || !nodeStack.empty()) {
while (root) {
nodeStack.push(root);
root = root->right;
}
if (!nodeStack.empty()) {
root = nodeStack.top(); nodeStack.pop();
if (++count == k) return root->val;
root = root->left;
}
}
return 0;
}
};