请用递归方式实现二叉树的先序、中序和后序的遍历打印。
给定一个二叉树的根结点root,请依次返回二叉树的先序,中序和后续遍历(二维数组的形式)。
/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } };*/ class TreeToSequence { public: vector<vector<int>> res; vector<int> vec; void PreOrder(TreeNode* root) { if(root) { vec.push_back(root->val); PreOrder(root->left); PreOrder(root->right); } return ; } void InOrder(TreeNode* root) { if(root) { InOrder(root->left); vec.push_back(root->val); InOrder(root->right); } return ; } void PostOrder(TreeNode* root) { if(root) { PostOrder(root->left); PostOrder(root->right); vec.push_back(root->val); } return ; } vector<vector<int> > convert(TreeNode* root) { // write code here if(root==NULL) return res; PreOrder(root); res.push_back(vec); vec.clear(); InOrder(root); res.push_back(vec); vec.clear(); PostOrder(root); res.push_back(vec); return res; } };