C++代码:
if(root)
{
if(root->left)
{
inorderTraversal(root->left);
}
num.push_back(root->val);
if(root->right)
{
inorderTraversal(root->right);
}
}
return num;
}
vector<int>num;
递归真的快。
C++代码:
stack<TreeNode*>sta;
vector<int>num;
TreeNode*l=root;
while(l||sta.size())
{
while(l)
{
sta.push(l);
l=l->left;
}
l=sta.top();
sta.pop();
num.push_back(l->val);
l=l->right;
}
return num;
非递归;