File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Recursive
2
+ // Runtime: 0 ms
3
+ class Solution {
4
+ public:
5
+ vector<int >res;
6
+
7
+ void inorder (TreeNode* root)
8
+ {
9
+ if (!root) return ;
10
+
11
+ inorder (root->left );
12
+ res.push_back (root->val );
13
+ inorder (root->right );
14
+ }
15
+ vector<int > inorderTraversal (TreeNode* root) {
16
+
17
+ if (!root) return res;
18
+ inorder (root);
19
+ return res;
20
+ }
21
+ };
22
+
23
+
24
+ // Iterative(Tutorial)
25
+ // Runtime: 0 ms
26
+ class Solution {
27
+ public:
28
+ vector<int > inorderTraversal (TreeNode* root) {
29
+ vector<int >res;
30
+ stack<TreeNode*> S;
31
+ TreeNode* C = root;
32
+
33
+ while (C!=NULL || !S.empty ())
34
+ {
35
+ while (C!=NULL )
36
+ {
37
+ S.push (C);
38
+ C = C->left ;
39
+ }
40
+
41
+ C = S.top ();
42
+ S.pop ();
43
+ res.push_back (C->val );
44
+ C = C->right ;
45
+ }
46
+
47
+ return res;
48
+ }
49
+ };
You can’t perform that action at this time.
0 commit comments