230. 二叉搜索树中第K小的元素
C代码:二叉树
int kthSmallest(struct TreeNode* root, int k){// struct TreeNode** stack = malloc(sizeof(struct TreeNode*) * 10000); // root 是结构体的地址struct TreeNode* stack[10000];int stkTop = 0;while (root != NULL || stkTop > 0) { // 中序遍历:存while (root != NULL) {stack[stkTop++] = root;root = root->left;}root = stack[--stkTop]; // 取--k;if (k == 0) {break;}root = root->right;}return root->val;
}