题解 | #实现二叉树先序,中序和后序遍历#
实现二叉树先序,中序和后序遍历
https://www.nowcoder.com/practice/a9fec6c46a684ad5a3abd4e365a9d362
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param root TreeNode类 the root of binary tree * @return int整型vector<vector<>> */ vector<int> pre,mid,post; void preOrder(TreeNode* root){ if(root == NULL) return; pre.push_back(root->val); preOrder(root->left); preOrder(root->right); } void midOrder(TreeNode* root){ if(root == NULL) return; midOrder(root->left); mid.push_back(root->val); midOrder(root->right); } void postOrder(TreeNode* root){ if(root == NULL) return; postOrder(root->left); postOrder(root->right); post.push_back(root->val); } vector<vector<int> > threeOrders(TreeNode* root) { // write code here if(root != nullptr){ preOrder(root); midOrder(root); postOrder(root); } vector<vector<int> >order = {pre,mid,post}; return order; } };