题目描述
给定一颗二叉搜索树,请找出其中的第k小的结点。
思路
中序遍历
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
private int count = 0;
TreeNode KthNode(TreeNode pRoot, int k) {
TreeNode tmp = null;
if (pRoot != null) {
tmp = KthNode(pRoot.left, k);
if (tmp != null) {
return tmp;
}
count++;
if (count == k) return pRoot;
tmp = KthNode(pRoot.right, k);
if (tmp != null) {
return tmp;
}
}
return null;
}
}