解题思路:二叉搜索树的中序遍历是可将结点升序排列的,可中序遍历后输出第k个结点。
import java.util.*; /* public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ public class Solution { public TreeNode KthNode(TreeNode pRoot, int k) { ArrayList<TreeNode> sort=new ArrayList<TreeNode>(); TreeNode node=null; if(k==0){ return node; } midOrder(pRoot,sort); if(k>sort.size()){ return node; } return sort.get(k-1); } public void midOrder(TreeNode root,ArrayList<TreeNode> sort){ if(root==null){ return; } midOrder(root.left,sort); sort.add(root); midOrder(root.right,sort); } }