We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 12f50ce commit dc22aa1Copy full SHA for dc22aa1
scripts/algorithms/B/Binary Tree Inorder Traversal/Binary Tree Inorder Traversal.cpp
@@ -1,12 +1,20 @@
1
+// Runtime: 4 ms (Top 33.36%) | Memory: 8.90 MB (Top 28.05%)
2
+
3
class Solution {
4
public:
- vector<int> v;
5
vector<int> inorderTraversal(TreeNode* root) {
- if(root!=NULL){
6
- inorderTraversal(root->left);
7
- v.push_back(root->val);
8
- inorderTraversal(root->right);
+ vector<int> nodes;
+ stack<TreeNode*> todo;
+ while (root || !todo.empty()) {
9
+ while (root) {
10
+ todo.push(root);
11
+ root = root -> left;
12
+ }
13
+ root = todo.top();
14
+ todo.pop();
15
+ nodes.push_back(root -> val);
16
+ root = root -> right;
17
}
- return v;
18
+ return nodes;
19
20
};
0 commit comments