二叉搜索树特性:
左孩子 < 根节点 < 右孩子
可以利用二叉树的中序遍历得到答案
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 { TreeNode KthNode(TreeNode pRoot, int k) { if (pRoot == null || k < 1) { return null; } List<TreeNode> list = new ArrayList<>(); treeDeep (pRoot, list); if (list.size() < k) { return null; } return list.get(k-1); } public void treeDeep (TreeNode pRoot, List<TreeNode> list) { if (pRoot == null) { return; } treeDeep(pRoot.left, list); list.add(pRoot); treeDeep(pRoot.right, list); } }