Binary Tree Inorder Traversal LeetCode solution in C++

1
class Solution {
2
public:
3
vector<int> inorderTraversal(TreeNode *root) {
4
vector<int> nodes;
5
stack<TreeNode *> todo;
6
while (root || !todo.empty()) {
7
while (root) {
8
todo.push(root);
9
root = root->left;
10
}
11
root = todo.top();
12
todo.pop();
13
nodes.push_back(root->val);
14
root = root->right;
15
}
16
return nodes;
17
}
18
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0