思路
- 中序遍历,二叉搜索树的中序遍历就是顺序排列
代码
import java.util.*;
public class Solution {
Stack<TreeNode> stack=new Stack<>();
TreeNode KthNode(TreeNode pRoot, int k){
if(pRoot==null){return null;}
KthNode(pRoot.left,k);
if(stack.size()<k){stack.push(pRoot);
}
KthNode(pRoot.right,k);
if(k==0||k>stack.size()){
return null;
}
return stack.peek();
}
} 
京公网安备 11010502036488号