-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
58 lines (53 loc) · 1.32 KB
/
Copy pathmain.cpp
File metadata and controls
58 lines (53 loc) · 1.32 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//
// main.cpp
// leetcode145
//
// Created by 萧天牧 on 17/4/23.
// Copyright © 2017年 萧天牧. All rights reserved.
//
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
/**
* Definition for a binary tree node.
*/
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
vector<int> preorderTraversal(TreeNode* root) {
vector<int> result;
if(!root)
return result;
stack<pair<TreeNode*, int>> sta;
sta.push(make_pair(root,0));
TreeNode* pNode = NULL;
while(!sta.empty()){
pNode = sta.top().first;
int time = sta.top().second;
sta.pop();
if(time == 0){
sta.push(make_pair(pNode,1));
if(pNode -> right)
sta.push(make_pair(pNode -> right,0));
if(pNode -> left)
sta.push(make_pair(pNode -> left,0));
}
else{
result.push_back(pNode -> val);
}
}
return result;
}
int main(int argc, const char * argv[]) {
TreeNode* root = new TreeNode(0);
root -> left =new TreeNode(1);
root -> left -> left =new TreeNode(2);
vector<int> res = preorderTraversal(root);
for(int i = 0 ; i< res.size(); i++)
cout <<res[i] <<" ";
return 0;
}