-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1-8.构造数组的MaxTree.cpp
136 lines (116 loc) · 2.8 KB
/
1-8.构造数组的MaxTree.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <stack>
#include <vector>
#include <map>
using namespace std;
// 二叉树节点定义
typedef struct node {
struct node* left;
struct node* right;
int val;
node( int data ) {
val = data;
left = NULL;
right = NULL;
}
} Node;
// 将弹栈元素存入map,记录映射关系
// 当栈为空时存入NULL
void popStackSetMap( stack<Node*> &s, map<Node*, Node*> &mp ) {
Node* curNode = s.top();
s.pop();
if( s.empty() ) {
mp.insert( map<Node*, Node*>::value_type( curNode, NULL ) );
}
else {
mp.insert( map<Node*, Node*>::value_type( curNode, s.top() ) );
}
}
// 建立 getMaxTree
Node* getMaxTree( vector<int> &vec ) {
vector<Node*> v;
for( int i = 0; i < vec.size(); i++ ) {
v.push_back( new Node( vec[i] ) );
}
stack<Node*> s;
map<Node*, Node*> mpLeft;
map<Node*, Node*> mpRight;
// 找左边第一个比当前元素大的值
for( int i = 0; i < v.size(); i++ ) {
Node* curNode = v[i];
while( !s.empty() && s.top()->val < curNode->val ) {
popStackSetMap( s, mpLeft );
}
s.push( curNode );
}
while( !s.empty() ) {
popStackSetMap( s, mpLeft );
}
// 找右边第一个比当前元素大的值
for( int i = vec.size() - 1; i >= 0; i-- ) {
Node* curNode = v[i];
while( !s.empty() && s.top()->val < curNode->val ) {
popStackSetMap( s, mpRight );
}
s.push( curNode );
}
while( !s.empty() ) {
popStackSetMap( s, mpRight );
}
// 建树,返回根节点
Node* head = NULL;
for( int i = 0; i < v.size(); i++ ) {
Node* curNode = v[i];
Node* curNodeLeft = mpLeft[curNode];
Node* curNodeRight = mpRight[curNode];
printf( "%d ", curNode->val );
if( curNodeLeft == NULL ) {
printf( "null " );
}
else {
printf( "%d ", curNodeLeft->val );
}
if( curNodeRight == NULL ) {
printf( "null\n" );
}
else {
printf( "%d\n", curNodeRight->val );
}
if( curNodeLeft == NULL && curNodeRight == NULL ) {
head = curNode;
}
else if( curNodeLeft == NULL ) {
if( curNodeRight->left == NULL ) curNodeRight->left = curNode;
else if( curNodeRight->right == NULL ) curNodeRight->right = curNode;
}
else if( curNodeRight == NULL ) {
if( curNodeLeft->left == NULL ) curNodeLeft->left = curNode;
else if( curNodeLeft->right == NULL ) curNodeLeft->right = curNode;
}
else {
Node* curMaxNode = curNodeLeft->val < curNodeRight->val ? curNodeLeft : curNodeRight;
if( curMaxNode->left == NULL ) curMaxNode->left = curNode;
else if( curMaxNode->right == NULL ) curMaxNode->right = curNode;
}
}
return head;
}
void printPre( Node* root ) {
if( root == NULL ) {
return;
}
printf( "%d\n", root->val );
printPre( root->left );
printPre( root->right );
}
int main() {
vector<int> vec;
vec.push_back( 3 );
vec.push_back( 5 );
vec.push_back( 4 );
vec.push_back( 1 );
vec.push_back( 2 );
Node* root = getMaxTree( vec );
printPre( root );
return 0;
}