Commit 0cb6a95d authored by yaozc's avatar yaozc
Browse files

problem 94, solution 2

parent 685e0412
......@@ -156,3 +156,29 @@ public:
private:
vector<int> result;
};
// second solution will be using a stack, non-recursive
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
TreeNode *ptr = root;
while(!mystack.empty() || ptr){
if(ptr){
mystack.push(ptr);
ptr = ptr->left;
}
else{
ptr = mystack.top();
result.push_back(ptr->val);
mystack.pop();
ptr = ptr->right;
}
}
return result;
}
private:
stack<TreeNode*> mystack;
vector<int> result;
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment