Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9663998

Browse files
Add C++ implementation
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent 98adda6 commit 9663998

File tree

5 files changed

+189
-0
lines changed

5 files changed

+189
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
/**
6+
* Definition for a binary tree node.
7+
* struct TreeNode {
8+
* int val;
9+
* TreeNode *left;
10+
* TreeNode *right;
11+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
12+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
13+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
14+
* };
15+
*/
16+
class Solution {
17+
public:
18+
int maxPathSum(TreeNode* root) {
19+
dfs(root);
20+
return max_sum;
21+
}
22+
23+
private:
24+
int max_sum = INT_MIN;
25+
int dfs(TreeNode *root) {
26+
if (root == nullptr) {
27+
return 0;
28+
}
29+
30+
int subl = max(0, dfs(root->left));
31+
int subr = max(0, dfs(root->right));
32+
int sum = root->val + subl + subr;
33+
max_sum = max(sum, max_sum);
34+
return root->val + max(subl, subr);
35+
}
36+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
/**
6+
* Definition for a binary tree node.
7+
* struct TreeNode {
8+
* int val;
9+
* TreeNode *left;
10+
* TreeNode *right;
11+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
12+
* };
13+
*/
14+
class Solution {
15+
public:
16+
int longestZigZag(TreeNode* root) {
17+
dfs(root);
18+
return maxzz - 1;
19+
}
20+
private:
21+
int maxzz = 0;
22+
pair<int, int> dfs(TreeNode *root) {
23+
if (root == nullptr) {
24+
return make_pair(0, 0);
25+
}
26+
27+
auto subl = dfs(root->left);
28+
auto subr = dfs(root->right);
29+
int sublzz = 1 + subl.second;
30+
int subrzz = 1 + subr.first;
31+
maxzz = max(maxzz, max(sublzz, subrzz));
32+
return make_pair(sublzz, subrzz);
33+
}
34+
};
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
/**
6+
* Definition for a binary tree node.
7+
* struct TreeNode {
8+
* int val;
9+
* TreeNode *left;
10+
* TreeNode *right;
11+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
12+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
13+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
14+
* };
15+
*/
16+
struct TreeInfo {
17+
bool isBst;
18+
int min_val;
19+
int max_val;
20+
int sum_val;
21+
TreeInfo() : isBst(true), min_val(INT_MAX), max_val(INT_MIN), sum_val(0) {}
22+
TreeInfo(bool bst, int min, int max, int sum) : isBst(bst), min_val(min), max_val(max), sum_val(sum) {}
23+
};
24+
25+
class Solution {
26+
public:
27+
int maxSumBST(TreeNode* root) {
28+
dfs(root);
29+
return max(0, max_sum);
30+
}
31+
private:
32+
int max_sum = INT_MIN;
33+
TreeInfo *dfs(TreeNode *root) {
34+
if (root == nullptr) {
35+
return new TreeInfo();
36+
}
37+
38+
auto subl = dfs(root->left);
39+
auto subr = dfs(root->right);
40+
41+
int sum = root->val + subl->sum_val + subr->sum_val;
42+
if (subl->isBst && subr->isBst && root->val > subl->max_val && root->val < subr->min_val) {
43+
max_sum = max(sum, max_sum);
44+
int min_val = min(root->val, subl->min_val);
45+
int max_val = max(root->val, subr->max_val);
46+
return new TreeInfo(true, min_val, max_val, sum);
47+
} else {
48+
return new TreeInfo(false, INT_MAX, INT_MIN, sum);
49+
}
50+
}
51+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
/**
6+
* Definition for a binary tree node.
7+
* struct TreeNode {
8+
* int val;
9+
* TreeNode *left;
10+
* TreeNode *right;
11+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
12+
* };
13+
*/
14+
class Solution {
15+
public:
16+
int diameterOfBinaryTree(TreeNode* root) {
17+
dfs(root);
18+
return max_diameter;
19+
}
20+
21+
private:
22+
int max_diameter = 0;
23+
int dfs(TreeNode* root) {
24+
if (root == nullptr) {
25+
return 0;
26+
}
27+
28+
int ld = dfs(root->left);
29+
int rd = dfs(root->right);
30+
max_diameter = max(max_diameter, ld + rd);
31+
return 1 + max(ld, rd);
32+
}
33+
};

‎563_binary_tree_tilt/tilt.cc‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
/**
6+
* Definition for a binary tree node.
7+
* struct TreeNode {
8+
* int val;
9+
* TreeNode *left;
10+
* TreeNode *right;
11+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
12+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
13+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
14+
* };
15+
*/
16+
class Solution {
17+
public:
18+
int findTilt(TreeNode* root) {
19+
dfs(root);
20+
return tilt;
21+
}
22+
23+
private:
24+
int tilt = 0;
25+
int dfs(TreeNode *root) {
26+
if (root == nullptr) {
27+
return 0;
28+
}
29+
30+
int subl = dfs(root->left);
31+
int subr = dfs(root->right);
32+
tilt += abs(subl - subr);
33+
return root->val + subl + subr;
34+
}
35+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /