Skip to content

Commit c621e9c

Browse files
committed
94. Binary Tree Inorder Traversal
1 parent 8cbc6a6 commit c621e9c

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

binary-tree-inorder-traversal.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
};

0 commit comments

Comments
 (0)