我的解题
#include <bits/stdc++.h>
using namespace std;
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode() :
val(0), left(nullptr), right(nullptr)
{
}
TreeNode(int x) :
val(x), left(nullptr), right(nullptr)
{
}
TreeNode(int x, TreeNode *left, TreeNode *right) :
val(x), left(left), right(right)
{
}
};
class Solution
{
int rank { 0 };
int res { 0 };
public:
int kthSmallest(TreeNode *root, int k)
{
traverse(root, k);
return res;
}
void traverse(TreeNode *root, int k)
{
if (root)
{
kthSmallest(root->left, k);
++rank;
if (rank == k)
{
res = root->val;
}
kthSmallest(root->right, k);
}
}
};
int main()
{
}
// g++ test.cpp --std=c++11 -pedantic -Wall -Wextra