題目:
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},
1
\
2
/
3
return [1,3,2].
代碼如下:
void getresult(TreeNode *root,vector<int> &result)
{
if(root==NULL)return;
if(root->left!=NULL)
{
getresult(root->left,result);
}
result.push_back(root->val);
if(root->right!=NULL)
{
getresult(root->right,result);
}
return;
}
vector<int> inorderTraversal(TreeNode *root) {
vector<int> result;
getresult(root,result);
return result;
}