Print Left View of Binary tree
// Print Left view of Binary tree
#include <iostream>
#include <vector>
using namespace std;
class tree {
public:
int data ;
tree * left, *right;
tree(int data)
{
this->data = data;
left = right = nullptr;
}
};
void printleftview(tree *node, int level)
{
static int maxlevel = 0;
if(node == nullptr)
{
return;
}
if(maxlevel < level)
{
maxlevel = level;
cout << node->data << endl;
}
printleftview(node->left, level+1);
printleftview(node->right, level+1);
}
int main()
{
cout<<"left view of binary tree" << endl;
//1
//2 3
// 4
tree* rootnode = new tree(1);
rootnode->left = new tree(2);
rootnode->left->left = new tree(3);
rootnode->left->right = new tree(6);
rootnode->left->right->right = new tree(7);
rootnode->right = new tree(4);
rootnode->right->right = new tree(5);
printleftview(rootnode , 1);
return 0;
}
//ans 1,2,3,7
Comments
Post a Comment