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]
.
Note: Recursive solution is trivial, could you do it iteratively?
confused what "{1,#,2,3}"
means? > read more on how binary tree is serialized on OJ.
class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> tmp;
traverse(root,tmp);
return tmp;
}
void traverse(TreeNode *root , vector<int> &tmp){
if(!root)
return;
if(root->left)
traverse(root->left,tmp);
if(root)
tmp.push_back(root->val);
if(root->right)
traverse(root->right,tmp);
}
};