题解 | #二叉搜索树的第k个结点#
二叉搜索树的第k个结点
http://www.nowcoder.com/practice/ef068f602dde4d28aab2b210e859150a
由于二叉搜索树的性质,左子节点小于根节点,右子节点大于根节点,中序遍历二叉树,即可从小到大排序
import java.util.*; public class Solution { ArrayList<TreeNode> list=new ArrayList(); TreeNode KthNode(TreeNode pRoot, int k) { inorder(pRoot,list); if(pRoot==null||k==0||k>list.size()){ return null; } return list.get(k-1); } public void inorder(TreeNode root,ArrayList<TreeNode> list){ if(root==null){ return; } inorder(root.left,list); list.add(root); inorder(root.right,list); } }