Skip to content

Commit 766f806

Browse files
committed
update
1 parent fcfa658 commit 766f806

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
vector<vector<int>> toadjlist(vector<vector<int>>&adj)
2+
{
3+
int n=adj.size();
4+
vector<vector<int>>adjlist(n);
5+
for(int i=0;i<n;i++)
6+
{
7+
for(int j=0;j<n;j++)
8+
{
9+
if(adj[i][j]==1)
10+
{
11+
adjlist[i].push_back(j);
12+
adjlist[j].push_back(i);
13+
}
14+
}
15+
}
16+
return adjlist;
17+
}

CP Templetes/trees/treebuilds.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
typedef long long ll;
4+
#define MOD 1000000007
5+
#define pb push_back
6+
#define ppb pop_back
7+
#define ff first
8+
#define ss second
9+
#define PI 3.141592653589793238462
10+
#define set_bits __builtin_popcountll
11+
#define all(x) (x).begin(), (x).end()
12+
#define debug(x) cerr<<x<<" ";
13+
class node{
14+
int val;
15+
node*left;
16+
node*right;
17+
node(int val)
18+
{
19+
this->val=val;
20+
this->left=nullptr;
21+
this->right=nullptr;
22+
}
23+
}
24+
node* buildtree(int n,vector<int>&v)
25+
{
26+
queue<node*>q;
27+
node*root=new node(v[0]);
28+
q.push(root);
29+
int i=1;
30+
while(i<n&&!q.empty())
31+
{
32+
node*temp=q.front();
33+
q.pop();
34+
if(v[i]!=-1)
35+
{
36+
temp->left=new node(v[i]);
37+
q.push(temp->left);
38+
}
39+
i++;
40+
41+
if(i<n)
42+
{
43+
if(v[i]!=-1)
44+
{
45+
temp->right=new node(v[i]);
46+
q.push(temp->right);
47+
}
48+
i++;
49+
}
50+
51+
}
52+
return root;
53+
}
54+
void levelordertreversal(node*root)
55+
{
56+
queue<node*>q;
57+
q.push(root);
58+
q.push(NULL);
59+
while(!q.empty())
60+
{
61+
node*temp=q.front();
62+
q.pop();
63+
if(temp==NULL)
64+
{
65+
cout<<endl;
66+
if(!q.empty())
67+
{
68+
q.push(NULL);
69+
}
70+
}
71+
else
72+
{
73+
cout<<root->val<<" ";
74+
if(root->left)
75+
{
76+
q.push(root->left);
77+
}
78+
if(root->right){
79+
q.push(root->right);
80+
}
81+
82+
}
83+
}
84+
}
85+
int main()
86+
{
87+
int n;
88+
cin>>n;
89+
vector<int>v;
90+
while(n!=-1)
91+
{
92+
v.push_back(n);
93+
cin>>n;
94+
}
95+
v.push_back(-1);
96+
node*root=buildtree(v.size(),v);
97+
levelordertreversal(root);
98+
}
99+

Important.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
13->int set/map we use upper and lower bound diff as set.upper_bound(3); in vector upper_bound(v.begin(),v.end(),target);because working in vector is BS and in set its tree
1414
14->int sum=accumulate(v.begin(),v.end(),0(initial sum value)); to get sum of vector;
1515
//7986952804
16+
sweepline adhoc

0 commit comments

Comments
 (0)