二叉搜索树的第K个节点_JAVA_简单
二叉搜索树的第k个结点
http://www.nowcoder.com/questionTerminal/ef068f602dde4d28aab2b210e859150a
- 二叉排序树找第K小值,则中序遍历到第k个时可满足需求
import java.util.*;
public class Solution {
TreeNode KthNode(TreeNode pRoot, int k) {
if(k == 0) {
return null;
}
Stack stack = new Stack();
TreeNode node = pRoot;
// 中序遍历
while(node != null || !stack.isEmpty()) {
while(node != null) {
stack.push(node);
node = node.left;
}
node = stack.pop();
if(--k <= 0) {
break;
}
node = node.right;
}
return node;
}
}
查看14道真题和解析


