94 Binary Tree Inorder Traversal
链接: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,#,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.
Hide Tags Tree Hash Table Stack
这个题目就是求二叉树的中序遍历。按照左中右的顺序遍历整棵树。
class Solution
{
public:
vector<int> inorderTraversal(TreeNode* root)
{
vector<int> result;
minordertraversal(root,result);
return result;
}
void minordertraversal(TreeNode* root,vector<int> &r)
{
if(root!=NULL)
{
minordertraversal(root->left,r);
r.push_back(root->val);
minordertraversal(root->right,r);
}
}
};