思路: 二叉搜索树,其特点是对于每个节点而言,其值比左孩子大,比右孩子小。即只需要对二叉搜索树进行中序遍历,即可得到从小到大的二叉树排列。
class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
int num=0;
stack<TreeNode*> st;
while(root||!st.empty()){
while(root){
st.push(root);
root=root->left;
}
root=st.top();
st.pop();
num++;
if(num==k)//返回第K个数
return root->val;
root=root->right;
}
return 0;
}
};