题解 | #实现二叉树先序,中序和后序遍历#
实现二叉树先序,中序和后序遍历
http://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; vector<int> in; vector<int> post; void preorder(TreeNode* root){ if(!root) return; pre.push_back(root->val); preorder(root->left); preorder(root->right); } void inorder(TreeNode* root){ if(!root) return; inorder(root->left); in.push_back(root->val); inorder(root->right); } void postorder(TreeNode* root){ if(!root) return; postorder(root->left); postorder(root->right); post.push_back(root->val); } vector<vector<int>> threeOrders(TreeNode* root) { // write code here vector<vector<int>> res; preorder(root); inorder(root); postorder(root); res.push_back(pre); res.push_back(in); res.push_back(post); return res; } };