/* public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ public class Solution { int count=0; TreeNode KthNode(TreeNode pRoot, int k) { //递归终止条件 if(pRoot==null){ return null; } //左子树中找符合要求的节点 TreeNode node=KthNode(pRoot.left,k); if(node!=null) return node; //根 if(++count==k) return pRoot; //右子树中找符合要求的节点 node=KthNode(pRoot.right,k); if(node!=null) return node; return null; } }
二叉搜索树按照中序遍历的顺序打印出来正好就是排序好的顺序。
按照中序遍历顺序找到第k个结点便可。