-
Notifications
You must be signed in to change notification settings - Fork 6
/
deepestLeftLeafNodeBinaryTree
73 lines (73 loc) · 1.77 KB
/
deepestLeftLeafNodeBinaryTree
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
64
65
66
67
68
69
70
71
72
73
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int val;
struct Node *left, *right;
};
struct ListNode
{
struct Node * data;
struct ListNode *next;
};
struct List
{
int level;
struct ListNode *head;
};
struct Node *newNode(int data)
{
struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
temp->val = data;
temp->left = temp->right = NULL;
return temp;
}
void deepestLeftLeafUtil(struct Node *root, int lvl, int isLeft,struct List *list)
{
if (root == NULL)
return;
if (isLeft == 1 && !root->left&& lvl >= list->level)
{
struct ListNode *temp = (struct ListNode*)malloc(sizeof(struct ListNode));
temp->data=root;
temp->next = NULL;
if(lvl == list->level)
{
temp->next=list->head;
}
else
{
//free(temp);
}
list->head=temp;
list->level = lvl;
}
deepestLeftLeafUtil(root->left, lvl+1, 1, list);
deepestLeftLeafUtil(root->right, lvl+1, 0, list);
}
void deepestLeftLeaf(struct Node *root)
{
struct List *list = (struct List *)malloc(sizeof(struct List));
list->level=0;
list->head=NULL;
struct ListNode *head = NULL;
deepestLeftLeafUtil(root, 0, 0, list);
for(head = list->head; head!=NULL ; head= head->next)
{
printf("%d ",head->data->val);
}
}
void main()
{
struct Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->right->left = newNode(5);
root->right->right = newNode(6);
root->right->left->right = newNode(7);
root->right->right->right = newNode(8);
root->right->left->right->left = newNode(9);
root->right->right->right->left = newNode(10);
deepestLeftLeaf(root);
}