1 2 3 4 5 6 7 8 9 10 11 12 13 14
| class Solution { public: void traversal(TreeNode* cur, vector<int>& vec) { if (cur == NULL) return; traversal(cur->left, vec); vec.push_back(cur->val); traversal(cur->right, vec); } vector<int> inorderTraversal(TreeNode* root) { vector<int> result; traversal(root, result); return result; } };
|
经典代码,递归调用遍历函数,前序、中序和后序的区别只在于遍历函数的顺序。