给出一棵二叉树,返回其中序遍历
样例
给出二叉树 {1,#,2,3}
,
1 \ 2 / 3
返回 [1,3,2]
.
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/*
* @param root: A Tree
* @return: Inorder in ArrayList which contains node values.
*/
vector<int> inorderTraversal(TreeNode * root) {
// write your code here
vector<int> temp;
if(root == NULL)
return temp;
inorder(root,temp);
return temp;
}
void inorder(TreeNode *root,vector<int> &path)
{
if(root != NULL)
{
inorder(root->left,path);
path.push_back(root->val);
inorder(root->right,path);
}
if(root == NULL)
return ;
}
};