二叉搜索树的中序遍历就是树节点值的递增排列!

// 注意当搜到第k个节点时,如何终止继续向下的无用搜索?
public class Solution {

    TreeNode treeNode = null;
    int count = 0;

    void dfs(TreeNode pRoot, int k) {

        if (count < k && pRoot.left != null) {
            dfs(pRoot.left, k);
        }

        if (++count == k) {
            treeNode = pRoot;
        }

        if (count < k && pRoot.right != null) {
            dfs(pRoot.right, k);
        }
    }

    TreeNode KthNode(TreeNode pRoot, int k) {
        if (pRoot == null || k <= 0) {
            return null;
        }

        dfs(pRoot, k);
        return treeNode;
    }
}