题目描述
TreeNode KthNode(TreeNode pRoot, int k) {
if(count > k || pRoot == null)
return null;
TreeNode p = pRoot;
Stack<TreeNode> stack = new Stack<>();
TreeNode kthNode = null;
stack.push(pRoot);
while(!stack.isEmpty()){
TreeNode tn = stack.peek();
while(tn != null){
tn = tn.left;
stack.push(tn);
}
stack.pop();
if (!stack.isEmpty()) {
tn = stack.pop();
count++;
if (count == k)
kthNode = tn;
stack.push(tn.right);
}
}
return kthNode;
}