Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include<queue>
- #include<string>
- using namespace std;
- class node{
- public:
- string data;
- node*left;
- node*right;
- node(string d){
- data = d;
- left = NULL;
- right = NULL;
- }
- };
- //preorder
- node* buildTree(){
- string d;
- cin>>d;
- if(d=="false"){
- return NULL;
- }
- if(d=="true"){
- cin>>d;
- }
- node* n = new node(d);
- n->left = buildTree();
- n->right = buildTree();
- return n;
- }
- int height(node*root){
- if(root==NULL){
- return 0;
- }
- int lh = height(root->left);
- int rh = height(root->right);
- return max(lh,rh)+1;
- }
- void preorder(node*root,vector<char>&v){
- if(root==NULL){
- v.push_back('n');
- return;
- }
- v.push_back('y');
- preorder(root->left,v);
- preorder(root->right,v);
- }
- int main(){
- node*root = NULL;
- node*root1 = NULL;
- root = buildTree();
- root1 = buildTree();
- vector<char> v;
- vector<char> v1;
- preorder(root,v);
- preorder(root1,v1);
- int x = height(root);
- int y = height(root1);
- if(x!=y){
- cout<<"false";
- }
- else if(x==y){
- if(v.size()!=v1.size()){
- cout<<"false";
- }
- else{
- for(int i=0;i<v.size();i++){
- if(v[i]!=v1[i]){
- cout<<"false";
- break;
- }
- }
- cout<<"true";
- }
- }
- }
Add Comment
Please, Sign In to add comment