aboutsummaryrefslogtreecommitdiff
path: root/kth_smallest_BST.cpp
blob: 73243d4e6904a1be271a76678b7c4fa67b43c89f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "leetcode.h"

class Solution {
public:
  set<int> s;
  int kthSmallest(TreeNode *root, int k) {
    traverse(root);
    for (auto ans : s) {
      k--;
      if (!k)
        return ans;
    }
    return 0;
  }

private:
  void traverse(TreeNode *root) {
    if (!root)
      return;
    s.insert(root->val);
    traverse(root->left);
    traverse(root->right);
  }
};

int main() {}