#include <iostream>
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
node* r;
node* l;
node(int data):data(data){};
};
void add(node*&root,vector<int>value,vector<char>direction)
{
assert(value.size()==direction.size());
node*curr=root;
for(int i=0;i<(int)value.size();i++)
{
if(direction[i]=='R')
{
if(curr->r)
assert(curr->r->data==value[i]);
else
{
node*n=new node(value[i]);
curr->r=n;
curr=curr->r;
}
}
else if(direction[i]=='L')
{
if(curr->l)
assert(curr->l->data==value[i]);
else
{
node*n=new node(value[i]);
curr->l=n;
curr=curr->l;
}
}
}
}
void print_inorder(node*curr)
{
if(!curr)
return;
print_inorder(curr->l);
cout<<curr->data<<" ";
print_inorder(curr->r);
}
int main()
{
node* root=new node(1);
add(root,{2,5},{'L','R'});
add(root,{2,4,6},{'L','L','L'});
add(root,{3,7},{'R','R'});
print_inorder(root);
return 0;
}
Hi everyone, I wrote this BT code to construct a tree and then printing it but it doesn’t print any values when I run the code, I couldn’t find the problem so I’d appreciate any help.
the add function should construct the tree.
the print fuction should print the tree inorder.
New contributor