binary-tree-traversal.cpp (826B)
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 * }; 11 */ 12 class Solution { 13 public: 14 15 void recurse(TreeNode* current, vector<int>& order) { 16 if(current == nullptr) { 17 return; 18 } 19 recurse(current->left, order); 20 order.push_back(current->val); 21 recurse(current->right, order); 22 return; 23 } 24 25 vector<int> inorderTraversal(TreeNode* root) { 26 // left, current, right 27 auto order = vector<int>{}; 28 recurse(root, order); 29 return order; 30 } 31 };