描述:
给出一棵二叉树,返回其中序遍历
样例:
给出二叉树 {1,#,2,3}
,
1 \ 2 / 3
返回 [1,3,2]
.
思路:
二叉树的前、中、后序遍历比较基础,用递归函数就好了~
AC代码:
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
/**
* @param root: The root of binary tree.
* @return: Inorder in vector which contains node values.
*/
public:
vector<int> v;
void mid(TreeNode *root)
{
if(root==NULL)
return;
else
{
mid(root->left);
v.push_back(root->val);
mid(root->right);
}
}
vector<int> inorderTraversal(TreeNode *root) {
// write your code here
mid(root);
return v;
}
};