Day116 | 灵神 | 二叉树 | 二叉搜索树中第K小的元素
Day116 | 灵神 | 二叉树 | 二叉搜索树中第K小的元素
230.二叉搜索树中第K小的元素
230. 二叉搜索树中第 K 小的元素 - 力扣(LeetCode)
思路:
这道题也比较简单,就是中序遍历到第K个就是答案了
class Solution {
public:int res=0;int count=0;void tra(TreeNode *t,int k){if(t==nullptr)return;tra(t->left,k);count++;if(k==count){res=t->val;return;}tra(t->right,k);}int kthSmallest(TreeNode* root, int k) {tra(root,k);return res;}
};