Given a Binary Tree rooted at root, the task is to find the sum of all leaf nodes which are left child of their parents and which also have their right sibling i.e, it’s a parent has also a right child.
Examples:
Input: 16
/ \
20 15
/ / \
100 21 25
/ / \ / \
27 14 9 7 6
/ \ \
17 12 2
Output: 24.
Explanation: The leaf nodes having values 7 and 17 only have a right sibling.
Input: 12
/ \
13 10
/ \
14 15
/ \
22 23
Output: 49
Approach: The idea is to use recursion to solve this problem. Start traversing from the root node. For every node check if it is NULL if true then return 0. If both children are NULL return 0. If both children are not NULL:
- If left is a leaf node then return root->left->data + value getting on traversing the right child.
- Else return value getting on traversing the left child + value getting on traversing the right child
Following the steps below to solve the problem:
- If root equals null or a leaf-node, then return 0.
- If root has both the children, then perform the following tasks:
- If the left child of root is a leaf node then return root->left->data + sumOfLeft(root->right).
- Else, return the summation of sumofLeft(root->left) and sumofLeft(root->right).
- Else if, there is root->left, then return sumofLeft(root->left).
- Else return sumofLeft(root->right).
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
Node *left, *right;
Node( int data)
{
this ->data = data;
this ->left = NULL;
this ->right = NULL;
}
};
int sumOfLeft(Node* root)
{
if (root == NULL)
return 0;
if (root->left == NULL
&& root->right == NULL)
return 0;
if (root->left != NULL
&& root->right != NULL) {
if (root->left->left == NULL
&& root->left->right == NULL) {
return root->left->data
+ sumOfLeft(root->right);
}
else {
return sumOfLeft(root->left)
+ sumOfLeft(root->right);
}
}
else if (root->left != NULL) {
return sumOfLeft(root->left);
}
return sumOfLeft(root->right);
}
int main()
{
Node* root = new Node(12);
root->left = new Node(13);
root->right = new Node(10);
root->right->left = new Node(14);
root->right->right = new Node(15);
root->right->right->left
= new Node(22);
root->right->right->right
= new Node(23);
cout << sumOfLeft(root);
return 0;
}
|
Java
import java.util.*;
class GFG{
static class Node {
int data;
Node left, right;
Node( int data)
{
this .data = data;
this .left = null ;
this .right = null ;
}
};
static int sumOfLeft(Node root)
{
if (root == null )
return 0 ;
if (root.left == null
&& root.right == null )
return 0 ;
if (root.left != null
&& root.right != null ) {
if (root.left.left == null
&& root.left.right == null ) {
return root.left.data
+ sumOfLeft(root.right);
}
else {
return sumOfLeft(root.left)
+ sumOfLeft(root.right);
}
}
else if (root.left != null ) {
return sumOfLeft(root.left);
}
return sumOfLeft(root.right);
}
public static void main(String[] args)
{
Node root = new Node( 12 );
root.left = new Node( 13 );
root.right = new Node( 10 );
root.right.left = new Node( 14 );
root.right.right = new Node( 15 );
root.right.right.left
= new Node( 22 );
root.right.right.right
= new Node( 23 );
System.out.print(sumOfLeft(root));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data;
self .left = None ;
self .right = None ;
def sumOfLeft(root):
if (root = = None ):
return 0 ;
if (root.left = = None and root.right = = None ):
return 0 ;
if (root.left ! = None and root.right ! = None ):
if (root.left.left = = None and root.left.right = = None ):
return root.left.data + sumOfLeft(root.right);
else :
return sumOfLeft(root.left) + sumOfLeft(root.right);
elif (root.left ! = None ):
return sumOfLeft(root.left);
return sumOfLeft(root.right);
if __name__ = = '__main__' :
root = Node( 12 );
root.left = Node( 13 );
root.right = Node( 10 );
root.right.left = Node( 14 );
root.right.right = Node( 15 );
root.right.right.left = Node( 22 );
root.right.right.right = Node( 23 );
print (sumOfLeft(root));
|
C#
using System;
public class GFG{
class Node {
public int data;
public Node left, right;
public Node( int data)
{
this .data = data;
this .left = null ;
this .right = null ;
}
};
static int sumOfLeft(Node root)
{
if (root == null )
return 0;
if (root.left == null
&& root.right == null )
return 0;
if (root.left != null
&& root.right != null ) {
if (root.left.left == null
&& root.left.right == null ) {
return root.left.data
+ sumOfLeft(root.right);
}
else {
return sumOfLeft(root.left)
+ sumOfLeft(root.right);
}
}
else if (root.left != null ) {
return sumOfLeft(root.left);
}
return sumOfLeft(root.right);
}
public static void Main(String[] args)
{
Node root = new Node(12);
root.left = new Node(13);
root.right = new Node(10);
root.right.left = new Node(14);
root.right.right = new Node(15);
root.right.right.left
= new Node(22);
root.right.right.right
= new Node(23);
Console.Write(sumOfLeft(root));
}
}
|
Javascript
<script>
class Node {
constructor(d) {
this .data = d;
this .left = null ;
this .right = null ;
}
};
function sumOfLeft(root) {
if (root == null )
return 0;
if (root.left == null
&& root.right == null )
return 0;
if (root.left != null
&& root.right != null ) {
if (root.left.left == null
&& root.left.right == null ) {
return root.left.data
+ sumOfLeft(root.right);
}
else {
return sumOfLeft(root.left)
+ sumOfLeft(root.right);
}
}
else if (root.left != null ) {
return sumOfLeft(root.left);
}
return sumOfLeft(root.right);
}
let root = new Node(12);
root.left = new Node(13);
root.right = new Node(10);
root.right.left = new Node(14);
root.right.right = new Node(15);
root.right.right.left
= new Node(22);
root.right.right.right
= new Node(23);
document.write(sumOfLeft(root));
</script>
|
Time Complexity: O(N) where N is the total number of nodes
Auxiliary Space: O(1)