-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.cpp
63 lines (55 loc) · 1.2 KB
/
tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
struct node
{
int data;
struct node *left, *right;
};
struct node *newnode(int item)
{
struct node *temp = new node;
temp->data = item;
temp->left = temp ->right = NULL;
return temp;
};
/*
void printpreordertree(struct node *root)
{
if (root == NULL)
return;
cout<< root->data << " ";
printpreordertree(root->left);
printpreordertree(root-> right);
}
*/
void leftviewutil(struct node *root, int level ,int *maxlevel)
{
if (root == NULL)
return;
if (level > *maxlevel)
{
*maxlevel = level;
cout << "Level:" << level << " " << root->data << endl;
}
leftviewutil(root->left, level+1, maxlevel);
leftviewutil(root->right, level+1, maxlevel);
}
void leftview(struct node *root)
{
int *maxlevel;
*maxlevel = -1;
leftviewutil(root, 0, maxlevel);
}
int main ()
{
struct node *root = newnode(12);
root->left = newnode(13);
root->right = newnode(14);
root->left->left = newnode(15);
root->left-> right = newnode(16);
leftview(root);
return 0;
}