/*
描述
分别按照二叉树先序,中序和后序打印所有的节点。
输入:
{1,2,3}
返回值:
[[1,2,3],[2,1,3],[2,3,1]]
备注:
n \leq 10^6n≤10^6
*/
#include<iostream> #include<vector> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; class Solution { public: vector<vector<int> > threeOrders(TreeNode* root) { preTraverse(root); midTraverse(root); postTraverse(root); result.push_back(preOrder); result.push_back(midOrder); result.push_back(postOrder); return result; } private: vector<int> preOrder; vector<int> midOrder; vector<int> postOrder; vector<vector<int> > result; void preTraverse(TreeNode* root) { if (root == nullptr) return; preOrder.push_back(root->val); preTraverse(root->left); preTraverse(root->right); } void midTraverse(TreeNode* root) { if (root == nullptr) return; midTraverse(root->left); midOrder.push_back(root->val); midTraverse(root->right); } void postTraverse(TreeNode* root) { if (root == nullptr) return; postTraverse(root->left); postTraverse(root->right); postOrder.push_back(root->val); } };