思路:二叉搜索树满足左树小于root,root小于右树,即符合中序遍历过程;
int kthSmallest(TreeNode* root, int k) {
stack<TreeNode*> st;
while(root || !st.empty()) {
if (root) {
st.push(root);
root = root->left;
} else {
TreeNode *temp = st.top();
k -= 1;
//中序遍历,k为0即为结果
if (k == 0) {
return temp->val;
}
st.pop();
root = temp->right;
}
}
return 0;
}