Sum of nodes in a binary tree having only the left child nodes
Last Updated :
13 Dec, 2022
Given a binary tree, the task is to find the sum of binary tree nodes having only the left child nodes.
Example:
Input: 8
/ \
3 7
/ \ /
5 6 0
/ /
1 2
Output: 18
Explanation: Nodes with values 5, 6, and 7 are the ones that have only the left child nodes
Input: 2
/ \
3 1
/ /
5 6
Output: 4
Approach: The given problem can be solved by traversing the binary tree using postorder traversal. The idea is to check if a node contains only a left child node, and add the value of the current node to the answer if the condition is true. Below steps can be followed to solve the problem:
- Traverse the binary tree using postorder traversal
- If the root is null, then return 0
- Use recursion on the left subtree and store its answer in a variable left
- Use recursion on the right subtree and store its answer in a variable right
- If root.left != null && root.right == null, then return the value of root.val + left + right
- Else return left + right
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int val;
Node *left;
Node *right;
Node( int value)
{
val = value;
left = NULL;
right = NULL;
}
};
int sumLeftChild(Node *root)
{
if (root == NULL)
return 0;
int left = sumLeftChild(root->left);
int right = sumLeftChild(root->right);
if (root->left != NULL && root->right == NULL)
{
return root->val + left + right;
}
return left + right;
}
int main()
{
Node *root = new Node(2);
root->left = new Node(3);
root->right = new Node(4);
root->left->left = new Node(5);
root->right->left = new Node(7);
cout << (sumLeftChild(root));
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static int sumLeftChild(Node root)
{
if (root == null )
return 0 ;
int left = sumLeftChild(root.left);
int right = sumLeftChild(root.right);
if (root.left != null && root.right == null ) {
return root.val + left + right;
}
return left + right;
}
public static void main(String[] args)
{
Node root = new Node( 2 );
root.left = new Node( 3 );
root.right = new Node( 4 );
root.left.left = new Node( 5 );
root.right.left = new Node( 7 );
System.out.println(sumLeftChild(root));
}
static class Node {
int val;
Node left, right;
public Node( int val)
{
this .val = val;
}
}
}
|
Python
class Node:
def __init__( self , value):
self .val = value
self .left = None
self .right = None
def sumLeftChild(root):
if root is None :
return 0
left = sumLeftChild(root.left)
right = sumLeftChild(root.right)
if root.left is not None and root.right is None :
return root.val + left + right
return left + right
if __name__ = = '__main__' :
root = Node( 2 )
root.left = Node( 3 )
root.right = Node( 4 )
root.left.left = Node( 5 )
root.right.left = Node( 7 )
print (sumLeftChild(root))
|
C#
using System;
public class GFG {
public static int sumLeftChild(Node root) {
if (root == null )
return 0;
int left = sumLeftChild(root.left);
int right = sumLeftChild(root.right);
if (root.left != null && root.right == null ) {
return root.val + left + right;
}
return left + right;
}
public static void Main(String[] args) {
Node root = new Node(2);
root.left = new Node(3);
root.right = new Node(4);
root.left.left = new Node(5);
root.right.left = new Node(7);
Console.WriteLine(sumLeftChild(root));
}
public class Node {
public int val;
public Node left, right;
public Node( int val) {
this .val = val;
}
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .val = val;
this .left = null ;
this .right = null ;
}
}
function sumLeftChild(root) {
if (root == null )
return 0;
let left = sumLeftChild(root.left);
let right = sumLeftChild(root.right);
if (root.left != null && root.right == null ) {
return root.val + left + right;
}
return left + right;
}
let root = new Node(2);
root.left = new Node(3);
root.right = new Node(4);
root.left.left = new Node(5);
root.right.left = new Node(7);
document.write(sumLeftChild(root));
</script>
|
Time Complexity: O(N)
Auxiliary space: O(n) for implicit call stack as using recursion
Share your thoughts in the comments
Please Login to comment...