0% found this document useful (0 votes)
12 views

Trees (Code)

The document contains C++ code for performing various tree traversals and operations including: level order traversal, reverse level order traversal, finding the height of a binary tree, and performing pre-order, in-order, and post-order traversals to return all tree traversals in a vector.

Uploaded by

prathamshetty874
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Trees (Code)

The document contains C++ code for performing various tree traversals and operations including: level order traversal, reverse level order traversal, finding the height of a binary tree, and performing pre-order, in-order, and post-order traversals to return all tree traversals in a vector.

Uploaded by

prathamshetty874
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

class Solution

{
public:
//Function to return the level order traversal of a tree.
vector<int> levelOrder(Node* node)
{
//Your code here
vector<int> v;
queue<Node*> queue;
queue.push(node);
while(!queue.empty())
{
Node *temp=queue.front();
v.push_back(temp->data);
if(temp->left!=NULL)
queue.push(temp->left);
if(temp->right!=NULL)
queue.push(temp->right);
queue.pop();
}
return v;
}
};
vector<int> reverseLevelOrder(Node *root)
{
// code here
vector<int> v;
queue<Node*> q;
q.push(root);
while(!q.empty())
{
Node *temp=q.front();
v.push_back(temp->data);
if(temp->right!=NULL)
q.push(temp->right);
if(temp->left!=NULL)
q.push(temp->left);
q.pop();

}
reverse(v.begin(),v.end());
return v;
}
//incorrect height of the tree
int c=0;
queue<Node*> q;
q.push(node);c++;
while(!q.empty())
{
Node* temp=q.front();
if(temp->right!=NULL || temp->left!=NULL)
c++;
if(temp->right!=NULL)
q.push(temp->right);
if(temp->left!=NULL)
q.push(temp->left);
q.pop();

}
return c;
//correct code
class Solution{
public:
//Function to find the height of a binary tree.
int height(struct Node* node){
// code here
if(node==NULL)
return 0;
int l=height(node->left);
int r=height(node->right);
return 1+max(l,r);

}
};
//traversals preoder,inorder,postorder
vector<int> v1;
vector<int> v2;
vector<int> v3;
void preorder(TreeNode *root)
{
vector<int> v;
if(root)
{
v1.push_back(root->data);
preorder(root->left);
preorder(root->right);
}

}
void inorder(TreeNode *root)
{
vector<int> v;
if(root)
{
inorder(root->left);
v2.push_back(root->data);
inorder(root->right);
}

}
void postorder(TreeNode *root)
{
vector<int> v;
if(root)
{

postorder(root->left);
postorder(root->right);
v3.push_back(root->data);
}

}
vector<vector<int>> getTreeTraversal(TreeNode *root){
// Write your code here.
vector<vector<int>> v;

preorder(root);
inorder(root);
postorder(root);
v.push_back(v2);
v.push_back(v1);
v.push_back(v3);
return v;
}

You might also like