题目链接:https://leetcode.com/problems/binary-tree-inorder-traversal/
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree [1,null,2,3]
,
1 \ 2 / 3
return [1,3,2]
.
Note: Recursive solution is trivial, could you do it iteratively?
class Solution{
public:
vector<int> inorderTraversal(TreeNode* root)
{
vector<int> array;
stack<TreeNode*> _stack;
TreeNode *p=root;
if(p==NULL)
return array;
while(!_stack.empty() || p!=NULL)
{
while(p!=NULL)
{
_stack.push(p);
p=p->left;
}
if(!_stack.empty())
{
p=_stack.top();
_stack.pop();
array.push_back(p->val);
p=p->right;
}
}
return array;
}
};