-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.cpp
More file actions
184 lines (151 loc) · 4.4 KB
/
tree.cpp
File metadata and controls
184 lines (151 loc) · 4.4 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include<bits/stdc++.h>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
struct State {
int min;
int max;
bool isValid;
State(int min, int max, bool isValid) : min(min), max(max), isValid(isValid) {}
};
class SolutionIsValidBSTS {
public:
State isValidBSTS(TreeNode * cur) {
State ret = State(cur->val, cur->val, true);
if(cur->left) {
State l = isValidBSTS(cur->left);
ret.min = min(l.min, ret.min);
ret.isValid = l.isValid && ret.isValid && l.max < cur->val;
}
if(cur->right) {
State r = isValidBSTS(cur->right);
ret.max = max(r.max, ret.max);
ret.isValid = r.isValid && ret.isValid && r.min > cur->val;
}
return ret;
}
bool isValidBST(TreeNode* root) {
if(!root) return true;
return isValidBSTS(root).isValid;
}
};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class SolutionOptimalIsValidBST {
public:
bool isValidBSTHelper(TreeNode* cur, TreeNode* &prev) {
if(!cur) return true;
bool ret = isValidBSTHelper(cur->left, prev);
if(!ret || (prev && prev->val >= cur->val)) return false;
prev = cur;
return isValidBSTHelper(cur->right, prev);
}
bool isValidBST(TreeNode* root) {
TreeNode * prev = NULL;
return isValidBSTHelper(root, prev);
}
};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode * l, TreeNode * r) {
if(l == NULL && r == NULL) return true;
if(!l || !r) return false;
return isSymmetric(l->left, r->right) && isSymmetric(l->right, r->left) && l->val == r->val;
}
bool isSymmetric(TreeNode* cur) {
return isSymmetric(cur, cur);
}
};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
struct State {
TreeNode * node;
int level;
State(TreeNode * node, int level) : node(node), level(level) {}
};
class SolutionLevelOrder {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int> > v;
queue<State> q;
if(root) q.push(State(root, 0));
while(q.size()) {
State cur = q.front();
q.pop();
if(cur.level >= v.size()) v.push_back(vector<int>());
v[cur.level].push_back(cur.node->val);
if(cur.node->left) q.push(State(cur.node->left, cur.level + 1));
if(cur.node->right) q.push(State(cur.node->right, cur.level + 1));
}
return v;
}
};
class SolutionZigzagLevelOrder {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int> > v;
queue<State> q;
if(root) q.push(State(root, 0));
while(q.size()) {
State cur = q.front();
TreeNode * l = cur.node->left;
TreeNode * r = cur.node->right;
int lvl = cur.level;
q.pop();
if(lvl >= v.size()) v.push_back(vector<int>());
v[lvl].push_back(cur.node->val);
if(l) q.push(State(l, lvl + 1));
if(r) q.push(State(r, lvl + 1));
}
for(int i = 0; i < v.size(); ++i) if(i & 1) reverse(v[i].begin(), v[i].end());
return v;
}
};
class SolutionMaxPathSum {
const int INF = 1000 * 1000 * 1000;
public:
int maxPathSum(TreeNode * cur, int & mx) {
if(!cur) return -INF;
int l = maxPathSum(cur->left, mx);
int r = maxPathSum(cur->right, mx);
int val = cur->val;
int mxSinglePath = max(val, val + max(l, r));
mx = max(mx, max(mxSinglePath, val + l + r));
return mxSinglePath;
}
int maxPathSum(TreeNode* cur) {
if(!cur) return 0;
int mx = cur->val;
maxPathSum(cur, mx);
return mx;
}
};
int main() {
}