非递归做法:
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
*/
class Solution {
public:
TreeNode* KthNode(TreeNode* pRoot, int k)
{
TreeNode* tmp = pRoot;
if(pRoot == NULL) return NULL;
stack<TreeNode*> q;
while(tmp != NULL || !q.empty()) {
if(tmp != NULL) {
q.push(tmp);
tmp = tmp->left;
}
else {
TreeNode* top = q.top();
q.pop();
k--;
if(k == 0) return top;
tmp = top->right;
}
}
return tmp;
}
};