给出一棵二叉树,返回其中序遍历
样例
样例 1:
输入:{1,2,3}
输出:[2,1,3]
解释:
1
/ \
2 3
它将被序列化为{1,2,3}
中序遍历
样例 2:
输入:{1,#,2,3}
输出:[1,3,2]
解释:
1
\
2
/
3
它将被序列化为{1,#,2,3}
中序遍历
挑战
你能使用非递归算法来实现么?
/**
* 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> ret;
vector<int> inorderTraversal(TreeNode * root) {
// write your code here
if(root != NULL)
{
inorderTraversal(root->left);
ret.push_back(root->val);
inorderTraversal(root->right);
}
return ret;
}
};