## The k-th largest node of the binary search tree

### Recursive method

The binary search tree sequence of the middle-order traversal is a monotonically increasing sequence, and the result of the middle-order traversal is placed in the vector, the k-th largest number is the number of the v.size ()-k position

```
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> v;
void dfs(TreeNode * root)
{
if(root->left) dfs(root->left);
v.push_back(root->val);
if(root->right) dfs(root->right);
}
int kthLargest(TreeNode* root, int k) {
dfs(root);
return v[v.size() - k];
}
};
```

### Iterative method

```
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int kthLargest(TreeNode* root, int k) {
stack<TreeNode *> st;
int i = 0;
while(!st.empty() || root)
{
while(root)
{
st.push(root);
root = root -> right;
}
root = st.top();
st.pop();
i++ ;
if(i == k) return root->val;
root = root->left;
}
return 0;
}
};
```