-
Notifications
You must be signed in to change notification settings - Fork 0
/
144.cpp
37 lines (33 loc) · 830 Bytes
/
144.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
//preorder最简单
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
if (!root) {
return vector<int> ();
}
vector<int> v_int;
stack<TreeNode *> s_t;
s_t.push(root);
while (!s_t.empty()) {
TreeNode *s_top = s_t.top();
v_int.push_back(s_top->val);
s_t.pop();
if (s_top->right) {
s_t.push(s_top->right);
}
if (s_top->left) {
s_t.push(s_top->left);
}
}
return v_int;
}
};