1 /* 2 struct TreeNode { 3 int val; 4 struct TreeNode *left; 5 struct TreeNode *right; 6 TreeNode(int x) : 7 val(x), left(NULL), right(NULL) { 8 } 9 }; 10 */ 11 class Solution { 12 public: 13 stack<TreeNode*>node; 14 TreeNode* KthNode(TreeNode* pRoot, unsigned int k) 15 { 16 if(pRoot == NULL||k<=0) 17 return NULL; 18 node.push(pRoot); 19 TreeNode *p = NULL; 20 while(!node.empty()) 21 { 22 while(pRoot->left != NULL) 23 { 24 pRoot=pRoot->left; 25 node.push(pRoot); 26 } 27 while(!node.empty()) 28 { 29 p = node.top(); 30 node.pop(); 31 --k; 32 if(k == 0) 33 return p; 34 if(p->right !=NULL) 35 { 36 node.push(p->right); 37 pRoot = p->right; 38 break; 39 } 40 } 41 42 } 43 44 return NULL; 45 } 46 47 48 };