-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeftLeafSumTree.cpp
70 lines (47 loc) · 1.16 KB
/
LeftLeafSumTree.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
64
65
66
67
68
69
70
@Chiranjeevi
Date: 30-July-17
http://www.geeksforgeeks.org/find-sum-left-leaves-given-binary-tree/
#include <iostream>
using namespace std;
struct node {
int data;
node *left;
node *right;
};
node *newNode(int data){
node *temp= new node;
temp->data=data;
temp->left=NULL;
temp->right=NULL;
return temp;
}
bool isLeaf(node *ptr){
if(ptr==NULL)
return false;
else if(ptr->left==NULL and ptr->right==NULL)
return true;
else
return false;
}
int leftLeafSum(node *root){
if(root==NULL)
return 0;
if(isLeaf(root->left))
return(root->left->data+leftLeafSum(root->right));
else
return(leftLeafSum(root->left)+leftLeafSum(root->right));
}
int main() {
node *root=NULL;
root=newNode(20);
root->left=newNode(9);
root->right=newNode(49);
root->left->left=newNode(5);
root->left->right=newNode(12);
root->right->left=newNode(23);
root->right->right=newNode(52);
root->left->right->right=newNode(15);
root->right->right->left=newNode(50);
cout<<leftLeafSum(root);
return 0;
}