import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param proot TreeNode类 * @param k int整型 * @return int整型 */ // 二叉搜索树的第k小的结点 // 思路:首先获取到中序排列的list,之后根据二叉搜索树中序排列有序的特性,list.get(k)就能找到第k小的节点值 // 注意要判断异常情况 List<TreeNode> list = new ArrayList<>(); public int KthNode (TreeNode proot, int k) { if(proot == null) return -1; // write code here InOrder(proot); int n = list.size(); if(k > n || k == 0) return -1; return list.get(k-1).val; } public void InOrder(TreeNode root){ if(root == null){ return; }else{ InOrder(root.left); list.add(root); InOrder(root.right); } } }