-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_tree_inorder_traversal.cpp
88 lines (76 loc) · 1.96 KB
/
binary_tree_inorder_traversal.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// https://oj.leetcode.com/problems/binary-tree-inorder-traversal/
// Date: Nov 15, 2014
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x): val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
vector<int> inorderTraversal(TreeNode *root)
{
vector<int> vec;
if (root == NULL)
{
return vec;
}
stack< pair<TreeNode*, int> > stk;
stk.push(make_pair(root,0));
while (!stk.empty())
{
pair<TreeNode*, int> elem = stk.top();
stk.pop();
TreeNode* node = elem.first;
int cnt = elem.second;
if (cnt == 0)
{
// We hadn't pushed left,right child of node on the stack before
if (node->right)
{
stk.push(make_pair(node->right,0));
}
if (node->left)
{
stk.push(make_pair(node,1));
stk.push(make_pair(node->left,0));
}
else
{
vec.push_back(node->val);
}
}
else
{
// We have already pushed left,right child of node on the stack before
vec.push_back(node->val);
}
}
return vec;
}
};
TreeNode* create_binary_tree()
{
TreeNode* root = new TreeNode(3);
TreeNode* node = new TreeNode(9);
root->left = node;
node = new TreeNode(20);
root->right = node;
TreeNode* leftNode = new TreeNode(15);
TreeNode* rightNode = new TreeNode(7);
node->left = leftNode;
node->right = rightNode;
return root;
}
int main(int argc, char* argv[])
{
TreeNode* root = create_binary_tree();
Solution sln;
vector<int> vec = sln.inorderTraversal(root);
return 0;
}