LeetCode 230. 二叉搜索树中第K小的元素

 //迭代法
class Solution {
    //定义一个变量,用于返回第K小的元素
    int res = 0;
    //定义计数变量
    int count = 0;
    public int kthSmallest(TreeNode root, int k) {
        search(root,k);
        return res;
    }
    public void search(TreeNode node, int k){
        if(node == null) return;
        search(node.left,k);
        count++;
        if(count == k){
            res = node.val;
            return;
        } 
        search(node.right,k);
    }
}
原文地址:https://www.cnblogs.com/peanut-zh/p/13882350.html