In the postorder traversal, the node will be removed from the stack only when the right sub-tree has been visited.so the path will be stored in the stack. we can keep check the SUM, the length from root to leaf node.

at leaf node, if SUM == sum, OK, return true. After postorder traversal, return false.

I have compared this solution with recursion solutions. In the leetcode OJ, the run time of two solutions is very near.

below is my iterator code.

```
class Solution {
public:
bool hasPathSum(TreeNode *root, int sum) {
stack<TreeNode *> s;
TreeNode *pre = NULL, *cur = root;
int SUM = 0;
while (cur || !s.empty()) {
while (cur) {
s.push(cur);
SUM += cur->val;
cur = cur->left;
}
cur = s.top();
if (cur->left == NULL && cur->right == NULL && SUM == sum) {
return true;
}
if (cur->right && pre != cur->right) {
cur = cur->right;
} else {
pre = cur;
s.pop();
SUM -= cur->val;
cur = NULL;
}
}
return false;
}
};
```