Solution Using Pbds


#1

`#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less_equal, rb_tree_tag,tree_order_statistics_node_update>

int Solution::kthsmallest(TreeNode* A, int B)
{
ordered_set s;
queue<TreeNode*> q;
q.push(A);
while(!q.empty())
{
TreeNode *p=q.front();
q.pop();
s.insert(p->val);
if(p->left!=NULL)
q.push(p->left);
if(p->right!=NULL)
q.push(p->right);
}
return (*s.find_by_order(B-1));
}`