Skip to content

Commit 1c03cca

Browse files
Add files via upload
1 parent eb07fd4 commit 1c03cca

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
void inorder(TreeNode *root , vector<int> &ans){
2+
3+
if(root==NULL)return ;
4+
5+
inorder(root->left,ans);
6+
if(root->left==NULL&&root->right==NULL){
7+
ans.push_back(root->val);
8+
}
9+
inorder(root->right,ans);
10+
}
11+
vector<int> Solution::solve(TreeNode* A) {
12+
vector<int> ans;
13+
TreeNode *root=A;
14+
while(root->left!=NULL&&root->right!=NULL){
15+
while(root->left!=NULL)
16+
{
17+
ans.push_back(root->val);
18+
root = root->left;
19+
}
20+
if(root->right!=NULL)
21+
{
22+
root = root->right;
23+
ans.push_back(root->val);
24+
}
25+
}
26+
vector<int>right_bount;
27+
root = A;
28+
while(root->left!=NULL&&root->right!=NULL){
29+
while(root->right!=NULL)
30+
{
31+
root = root->right;
32+
right_bount.push_back(root->val);
33+
}
34+
if(root->left!=NULL)
35+
{
36+
root = root->left;
37+
right_bount.push_back(root->val);
38+
}
39+
}
40+
root = A;
41+
inorder(root,ans);
42+
reverse(right_bount.begin(),right_bount.end());
43+
44+
ans.insert(ans.end(),right_bount.begin()+1,right_bount.end());
45+
return ans;
46+
}

Trees/Equal Tree Partition.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
long f(TreeNode* x) {
2+
return x?x->val+f(x->left)+f(x->right):0L;
3+
}
4+
long g(TreeNode* x, long s, bool& res) {
5+
if(!x)return 0;
6+
long a = g(x->left, s, res);
7+
long b = g(x->right, s, res);
8+
if(x->left&&a==s)res=true;
9+
if(x->right&&b==s)res=true;
10+
return a+x->val+b;
11+
}
12+
int Solution::solve(TreeNode* A) {
13+
long s = f(A);
14+
if(!A||(s&1))return 0;
15+
bool res=false;
16+
g(A,s/2,res);
17+
return res;
18+
}

Trees/Largest BST Subtree.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
struct Info {
2+
int sz;
3+
int mx;
4+
int mn;
5+
int ans;
6+
bool isBST;
7+
};
8+
9+
Info largestBSTinBinaryTree(TreeNode* root) {
10+
if (root == NULL)
11+
return {0, INT_MIN, INT_MAX, 0, true};
12+
13+
if (root->left == NULL && root->right == NULL)
14+
return {1, root->val, root->val, 1, true};
15+
16+
Info l = largestBSTinBinaryTree(root->left);
17+
Info r = largestBSTinBinaryTree(root->right);
18+
19+
Info ret;
20+
ret.sz = (1 + l.sz + r.sz);
21+
22+
if (l.isBST && r.isBST && l.mx < root->val && r.mn > root->val) {
23+
ret.mn = min(l.mn, min(r.mn, root->val));
24+
ret.mx = max(r.mx, max(l.mx, root->val));
25+
ret.ans = ret.sz;
26+
ret.isBST = true;
27+
return ret;
28+
}
29+
30+
ret.ans = max(l.ans, r.ans);
31+
ret.isBST = false;
32+
return ret;
33+
}
34+
35+
36+
int Solution::solve(TreeNode* A) {
37+
Info res=largestBSTinBinaryTree(A);
38+
return res.ans;
39+
}
40+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
int ans=0;
2+
3+
void dfs(TreeNode *root,int v,int out) {
4+
if (!root)
5+
return ;
6+
if (root->val==v+1)
7+
++out ;
8+
else
9+
out=1 ;
10+
ans=max(ans, out);
11+
dfs(root->left,root->val,out);
12+
dfs(root->right,root->val,out);
13+
}
14+
int longestConsecutive(TreeNode* root) {
15+
if (!root)
16+
return 0;
17+
ans=0;
18+
dfs(root,root->val, 0);
19+
return ans;
20+
}
21+
22+
int Solution::solve(TreeNode* A) {
23+
return longestConsecutive(A);
24+
}

0 commit comments

Comments
 (0)