根据二叉搜索的性质可知,中序遍历即位从小到大顺序,可以使用中序遍历将其存起来取出目标节点,也可直接中序遍历同时得到节点
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
int index = 0;
TreeNode res = null;
TreeNode KthNode(TreeNode pRoot, int k) {
if(pRoot==null||k == 0){
return null;
}
KthNode(pRoot.left,k);
index++;
if(index == k){
res = pRoot;
}
KthNode(pRoot.right,k);
return res;
}
} 


京公网安备 11010502036488号