public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
import java.util.*;
public class Solution {
    TreeNode KthNode(TreeNode pRoot, int k) {
        //中序遍历 第 k 个数
        if(pRoot == null){
            return null;
        }
        Stack<TreeNode> stack = new Stack<>();
        int n = 0;
        TreeNode current = pRoot;
        while(!stack.isEmpty() || current != null){
            //该节点的左子树都入栈
            while(current != null){
                stack.push(current);
                current = current.left;
            }
            current = stack.pop();//栈顶 左子树 最左元素
            n++;
            if(n == k){
                return current;
            }
            //取出左节点后, 中间根节点,第二次就是右节点了
            current = current.right;
        }
        
        return null;
    }
//     private void inOrder(TreeNode node, int k){
//         if(node == null){
//             return ;
//         }
//         inOrder(node.left,k);
//         k --;
//         if(k == 0){
//             result = root;
            
//         }
//         inOrder(node.right,k);
//     }


}