summaryrefslogtreecommitdiff
path: root/top-interview-questions/medium/trees_and_graphs/01_binary_tree_inorder_traversal.cc
diff options
context:
space:
mode:
Diffstat (limited to 'top-interview-questions/medium/trees_and_graphs/01_binary_tree_inorder_traversal.cc')
-rw-r--r--top-interview-questions/medium/trees_and_graphs/01_binary_tree_inorder_traversal.cc27
1 files changed, 27 insertions, 0 deletions
diff --git a/top-interview-questions/medium/trees_and_graphs/01_binary_tree_inorder_traversal.cc b/top-interview-questions/medium/trees_and_graphs/01_binary_tree_inorder_traversal.cc
new file mode 100644
index 0000000..6a2762b
--- /dev/null
+++ b/top-interview-questions/medium/trees_and_graphs/01_binary_tree_inorder_traversal.cc
@@ -0,0 +1,27 @@
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 */
12class Solution {
13public:
14 void inorder(TreeNode* node, vector<int>& vals) {
15 if (node == nullptr) return;
16
17 inorder(node->left, vals);
18 vals.push_back(node->val);
19 inorder(node->right, vals);
20 }
21
22 vector<int> inorderTraversal(TreeNode* root) {
23 vector<int> vals;
24 inorder(root, vals);
25 return vals;
26 }
27};