ZigZag level order traversal BT - c++11

I am tried to this question for so many times but I don't know what is wrong in my code,
please help me find a bug in this,
problem reference:
https://www.interviewbit.com/problems/zigzag-level-order-traversal-bt/
https://www.geeksforgeeks.org/zigzag-tree-traversal/
vector<vector<int>> Solution::zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> vf;
vector<int> v;
if(A==NULL){
return vf;
}
TreeNode* temp=A;
bool turn=false;
stack<TreeNode*> s1;
stack<TreeNode*> s2;
s1.push(temp);
while(!s1.empty()||!s2.empty()){
if(turn==false){
while(!s1.empty()){
TreeNode* node=s1.top();
v.push_back(node->val);
if(node->left!=NULL){
s2.push(node->left);
}
if(node->right!=NULL){
s2.push(node->right);
}
s1.pop();
}
if(s1.empty()){
turn=true;
}
}
else if(turn==true){
while(!s2.empty()){
TreeNode* node=s2.top();
v.push_back(node->val);
if(node->left!=NULL){
s1.push(node->right);}
if(node->right!=NULL){
s1.push(node->left);}
s2.pop();
}
if(s2.empty()){
turn=false;
}
}
vf.push_back(v);
v.clear();
}
return vf;
}

Related

Getting segmentation fault while implementing Kosaraju Algorithm

kosaraju algorithm:
Problem statement: Given a Directed Graph with V vertices (Numbered from 0 to V-1) and E edges, Find the number of strongly connected components in the graph.
while implementing this code I am getting a segmentation fault. Where is the error in this code?
void dfs(int node,vector<int>adj[],stack<int>&st,vector<int>&vis){
vis[node]=1;
for(auto it: adj[node]){
if(!vis[it]){
dfs(it,adj,st,vis);
}
}
st.push(node);
}
void revdfs(int node,vector<int>trs[],vector<int>&vis){
vis[node]=1;
for(auto it: trs[node]){
if(!vis[it]){
revdfs(it,trs,vis);
}
}
}
int kosaraju(int V, vector<int> adj[])
{
stack<int>st;
vector<int>vis(V+1,0);
for(int i=0; i<V;i++){
if(!vis[i])
dfs(i,adj,st,vis);
}
int cnt=0;
vector<int> transpose[V+1];
for(int i = 1;i<=V;i++) {
vis[i] = 0;
for(auto it: adj[i]) {
transpose[it].push_back(i);
}
}
while(!st.empty()){
int node=st.top();
st.pop();
if(!vis[node]){
cnt++;
revdfs(node,transpose,vis);
}
}
return cnt;

distance between any two nodes in a graph(based on edges between them)

The below code in c++ is to find the distance between any two nodes in a graph but there is some logical error where I tried to find but can't. Everything seems to be perfect but I can say that there is something wrong with a break inside if condition inside for loop which is inside while(s.!empty()).
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int find_distance(int link[100][100],int start,int dest,int n,int e)
{
start=start-1;
dest=dest-1;
if(start==dest)return 0;
int visited[100];
int distance;
for(int i=0;i<n;i++)
{
visited[i]=0;
}
stack<int>s;
int k;s.push(start);
visited[start]=1;
bool found=false;
int count =0;
while(!s.empty())
{
k=s.top();
int i;
int check_point=1;
for(i=0;i<n;i++)
{
if(link[k][i]==1 && visited[i]!=1)
{
s.push(i);
count ++;
check_point=0;
if(i==dest)
{
cout<<"found";
found=true;
}
break;
}
}
if(check_point==1)
{
s.pop();
count--;
}
if(found)break;
}
return count;
}
int main()
{
int n,e,a,b;
int link[100][100];
cout<<"enter the number of vertices and edges";
// cin>>n>>e;
n=5;e=4;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==j)
link[i][j]=0;
else
link[i][j]=INT_MAX;
cout<<link[i][j]<<" ";
}
cout<<endl;
}
int input[4][2]={{1,2},{1,3},{2,4},{2,5}};
for(int i=0;i<e;i++)
{
a=input[i][0];
b=input[i][1];
link[a-1][b-1]=1;
link[b-1][a-1]=1;
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<link[i][j]<<" ";
}
cout<<endl;
}
while(true) {
cout<<endl<<"enter the starting point .. ";
int start;
cin>>start;
int dest;
cout<<endl<<"enter the destination point .. ";
cin>>dest;
int distance = find_distance(link,start,dest,n,e);
cout<<endl<<"distance is "<<distance;
}
return 0;
}

deriving time-complexity of nqueens problem

Can anyone prove/derive the time complexity of my nqueens solution approach?
I am going through each and every position on the grid and if it is possible to place a queen there, then, I am calculating solution by first placing the queen and then unplacing the queen, else I move on.
Code:
bool notinrow(int row,int col,vector<string> tra)
{
for(int i=0;i<tra.size();i++)
{
if(tra[row][i]=='Q' & i!=col)
return false;
}
return true;
}
bool notincol(int row,int col,vector<string> tra)
{
for(int j=0;j<tra.size();j++)
{
if(tra[j][col]=='Q' & j!=row)
return false;
}
return true;
}
bool notindiag1(int r,int c, vector<string> tra)
{
int i=r-1;
int j=c-1;
while(i>=0 & j>=0)
{
if(tra[i][j]=='Q')
return false;
i--;j--;
}
return true;
}
bool notindiag2(int r, int c,vector<string> tra)
{ int i=r-1;
int j=c+1;
while(i>=0 & j<totqueens)
{
if(tra[i][j]=='Q')
return false;
i--;j++;
}
return true;
}
void nqueens(int number,vector<string> tra,int currqueens)
{
if(currqueens==totqueens)
{
bhej.push_back(tra);
return ;
}
if(number==totiter)
return;
int x=number/totqueens;
int y=number%totqueens;
if(ispossible(x,y,tra))
{
tra[x][y]='Q';
nqueens(number+1,tra,currqueens+1);
tra[x][y]='.';
nqueens(number+1,tra,currqueens) ;
}
else
nqueens(number+1,tra,currqueens);
}```

Why Numbers are getting printed in the form like "12-256" , "13-256" rather than 12,13 etc.?

I'm solving one of the algorithms problem from university to implemet queue using stacks.
I've got my logic right i guess but the numbers are getting printed in the form of 12-256, 13-256, 14-256 instead of 12,13,14.
Here's my C++ Code,
#include <iostream>
using namespace std;
class Stack{
private:
int arr[200];
int tos = -1;
public:
bool empty(){
return (tos == -1)?true:false;
}
void push(int element){
arr[++tos] = element;
}
int pop(){
return arr[tos--];
}
void show(){
if(tos == -1){
cout<<"stack empty";
}else{
for(int i=tos;i>0;i--)
cout<<arr[i]<<"\t";
}
}
};
class Queue{
private:
Stack s1,s2;
public:
void enQueue(int x){
//just using s1 to add new elements
s1.push(x);
}
int deQueue(){
if(s1.empty())
throw 'e';
else{
int e;
while(!s1.empty()){
e = s1.pop();
s2.push(e);
}
cout<<"\nelement to be removed:"<<s2.pop();
if(s2.empty())
throw 'f';
else{
int e;
while(!s2.empty()){
e = s2.pop();
s1.push(e);
}
}
}
}
};
int main()
{
try{
Queue q1;
q1.enQueue(12);
q1.enQueue(13);
q1.enQueue(14);
q1.enQueue(15);
cout<<q1.deQueue();
cout<<q1.deQueue();
cout<<q1.deQueue();
cout<<q1.deQueue();
}catch(char c){
cout<<"\nstack empty!";
}
return 0;
}
I'm basically a Python Guy so i'm not able to figure out what's wrong with this code.
I'm new to C++, so please guide me through this.
Thanks in advance!
deQueue suffers from the following problems.
It doesn't return anything.
It's OK for s2 to be empty after its top has been popped.
Here's an updated version that should work.
int deQueue(){
if(s1.empty())
throw 'e';
int e;
while(!s1.empty()){
e = s1.pop();
s2.push(e);
}
int ret = s2.pop();
cout<<"\nelement dequeued:"<< ret;
// This is not correct.
// It's OK for s2 to be empty after its top has been popped.
// if(s2.empty())
// throw 'f';
while(!s2.empty()){
e = s2.pop();
s1.push(e);
}
return ret;
}
Suggestion for further improvement
Queue does not need two Stack objects as member variables. s2 can be a function local variable in deQueue.
class Queue
{
private:
Stack s;
...
};
If you decide to make that change, you'll have to update enQueue and deQueue accordingly.

Hierholzer algorithm to find eulerian path [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have implemented hierholzer algorithm to find eulerian path in a graph using two stacks. Below is my implementation. There is some runtime error, will be glad if somebody could help
#include<bits/stdc++.h>
using namespace std;
stack<int> result;
stack<int> temp;
class graph
{
int v;
list<int> *adj;
public:
graph(int v)
{
this->v=v;
adj=new list<int> [v];
}
~graph()
{
delete []adj;
}
void add_edge(int u,int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}
void remove_edge(int u, int v);
int start_vertex();
void print_euler_path(int u);
bool allvisited();
};
int graph::start_vertex()
{
int u=0;
for(int u=0;u<v;u++)
{
if(adj[u].size() & 1)
break;
}
return u;
}
bool graph::allvisited()
{
for(int i=0;i<v;i++)
{
if(adj[i].size()>0)
{
list<int>::iterator it;
for(it=adj[i].begin();it!=adj[i].end();it++)
{
if(*it!=-1)
return false;
}
}
}
return true;
}
void graph::remove_edge(int u,int v)
{
list<int>::iterator i;
i=find(adj[u].begin(),adj[u].end(),v);
*i=-1;
i=find(adj[v].begin(),adj[v].end(),u);
*i=-1;
}
void graph::print_euler_path(int u)
{
temp.push(u);
list<int>::iterator i;
int flag=0;
if(allvisited())
return;
for(i=adj[u].begin();i!=adj[u].end();i++)
{
if(*i!=-1)
{
cout<<"S";
remove_edge(u,*i);
print_euler_path(*i);
}
}
if(!temp.empty())
{
int k=temp.top();
temp.pop();
result.push(k);
if(!temp.empty())
print_euler_path(temp.top());
}
}
int main()
{
graph g(6);
g.add_edge(0,1);
g.add_edge(1,2);
g.add_edge(2,3);
g.add_edge(3,0);
g.add_edge(5,1);
g.add_edge(5,2);
g.add_edge(4,1);
g.add_edge(4,2);
int u=g.start_vertex();
g.print_euler_path(u);
while(!result.empty())
{
cout<<result.top()<<" ";
result.pop();
}
return 0;
}
For exact logic you can refer http://iampandiyan.blogspot.in/2013/10/c-program-to-find-euler-path-or-euler.html
I don't think that these lines do what you want:
remove_edge(u,*i);
print_euler_path(*i);

Resources