C++简洁代码:
class Solution {
public:
TreeNode* KthNode(TreeNode* pRoot, int k) {
stack<TreeNode*> stk;
TreeNode* cur = pRoot;
while(cur || stk.size()) {
while(cur) {
stk.push(cur);
cur = cur->left;
}
cur = stk.top();
stk.pop();
if(-- k == 0) return cur;
cur = cur->right;
}
return nullptr;
}
};
京公网安备 11010502036488号