题解 | #二叉搜索树的第k个结点#
二叉搜索树的第k个结点
http://www.nowcoder.com/practice/ef068f602dde4d28aab2b210e859150a
中序遍历解决问题
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
import java.util.ArrayList;
public class Solution {
ArrayList<TreeNode> save = new ArrayList<>();
TreeNode KthNode(TreeNode pRoot, int k) {
if(pRoot == null){
return null;
}
inorder(pRoot);
if(k <= 0 || k > save.size()){
return null;
}
return save.get(k-1);
}
public ArrayList<TreeNode> inorder(TreeNode root){
if(root == null){
return null;
}
if(root.left != null){
inorder(root.left);
}
save.add(root);
if(root.right != null){
inorder(root.right);
}
return save;
}
} 