Sum of nodes in the left view of the given binary tree
Given a binary tree, the task is to find the sum of the nodes which are visible in the left view. The left view of a binary tree is the set of nodes visible when the tree is viewed from the left.
Examples:
Input:
1
/ \
2 3
/ \ \
4 5 6
Output: 7
1 + 2 + 4 = 7
Input:
1
/ \
2 3
\
4
\
5
\
6
Output: 18
Approach: The problem can be solved using simple recursive traversal. We can keep track of the level of a node by passing a parameter to all the recursive calls. The idea is to keep track of the maximum level also and traverse the tree in a manner that the left subtree is visited before the right subtree. Whenever a node whose level is more than the maximum level so far is encountered, add the value of the node to the sum because it is the first node in its level (Note that the left subtree is traversed before the right subtree).
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* newNode( int item)
{
Node* temp = new Node();
temp->data = item;
temp->left = temp->right = NULL;
return temp;
}
void sumLeftViewUtil(Node* root, int level, int * max_level, int * sum)
{
if (root == NULL)
return ;
if (*max_level < level) {
*sum += root->data;
*max_level = level;
}
sumLeftViewUtil(root->left, level + 1, max_level, sum);
sumLeftViewUtil(root->right, level + 1, max_level, sum);
}
void sumLeftView(Node* root)
{
int max_level = 0;
int sum = 0;
sumLeftViewUtil(root, 1, &max_level, &sum);
cout << sum;
}
int main()
{
Node* root = newNode(12);
root->left = newNode(10);
root->right = newNode(30);
root->right->left = newNode(25);
root->right->right = newNode(40);
sumLeftView(root);
return 0;
}
|
Java
class Node {
int data;
Node left, right;
public Node( int item)
{
data = item;
left = right = null ;
}
}
class BinaryTree {
Node root;
static int max_level = 0 ;
static int sum = 0 ;
void sumLeftViewUtil(Node node, int level)
{
if (node == null )
return ;
if (max_level < level) {
sum += node.data;
max_level = level;
}
sumLeftViewUtil(node.left, level + 1 );
sumLeftViewUtil(node.right, level + 1 );
}
void sumLeftView()
{
sumLeftViewUtil(root, 1 );
System.out.print(sum);
}
public static void main(String args[])
{
BinaryTree tree = new BinaryTree();
tree.root = new Node( 12 );
tree.root.left = new Node( 10 );
tree.root.right = new Node( 30 );
tree.root.right.left = new Node( 25 );
tree.root.right.right = new Node( 40 );
tree.sumLeftView();
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def sumLeftViewUtil(root, level, max_level, sum ):
if root is None :
return
if (max_level[ 0 ] < level):
sum [ 0 ] + = root.data
max_level[ 0 ] = level
sumLeftViewUtil(root.left, level + 1 , max_level, sum )
sumLeftViewUtil(root.right, level + 1 , max_level, sum )
def sumLeftView(root):
max_level = [ 0 ]
sum = [ 0 ]
sumLeftViewUtil(root, 1 , max_level, sum )
print ( sum [ 0 ])
root = Node( 12 )
root.left = Node( 10 )
root.right = Node( 20 )
root.right.left = Node( 25 )
root.right.right = Node( 40 )
sumLeftView(root)
|
C#
using System;
public class Node {
public int data;
public Node left, right;
public Node( int item)
{
data = item;
left = right = null ;
}
}
public class BinaryTree {
public Node root;
public static int max_level = 0;
public static int sum = 0;
public virtual void leftViewUtil(Node node, int level)
{
if (node == null ) {
return ;
}
if (max_level < level) {
sum += node.data;
max_level = level;
}
leftViewUtil(node.left, level + 1);
leftViewUtil(node.right, level + 1);
}
public virtual void leftView()
{
leftViewUtil(root, 1);
Console.Write(sum);
}
public static void Main( string [] args)
{
BinaryTree tree = new BinaryTree();
tree.root = new Node(12);
tree.root.left = new Node(10);
tree.root.right = new Node(30);
tree.root.right.left = new Node(25);
tree.root.right.right = new Node(40);
tree.leftView();
}
}
|
Javascript
<script>
class Node {
constructor(item)
{
this .data = item;
this .left = null ;
this .right = null ;
}
}
var root = null ;
var max_level = 0;
var sum = 0;
function leftViewUtil(node, level)
{
if (node == null ) {
return ;
}
if (max_level < level) {
sum += node.data;
max_level = level;
}
leftViewUtil(node.left, level + 1);
leftViewUtil(node.right, level + 1);
}
function leftView()
{
leftViewUtil(root, 1);
document.write(sum);
}
root = new Node(12);
root.left = new Node(10);
root.right = new Node(30);
root.right.left = new Node(25);
root.right.right = new Node(40);
leftView();
</script>
|
Time Complexity: O(N) where N is number of nodes in a given binary tree
Auxiliary Space: O(N)
Iterative Approach(Using Queue):
Follow the below steps to solve the above problem:
- Simply traverse the whole binary tree in level Order Traversal with the help of Queue data structure.
- At each level keep track of the sum and add first node data of each level in sum.
- Finally, print the sum.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
struct Node{
int data;
Node* left;
Node* right;
Node( int data){
this ->data = data;
this ->left = this ->right = NULL;
}
};
struct Node* newNode( int data){
return new Node(data);
}
void sumLeftView(Node* root){
if (root == NULL) return ;
queue<Node*> q;
q.push(root);
int sum = 0;
while (!q.empty()){
int n = q.size();
for ( int i = 0; i<n; i++){
Node* front_node = q.front();
q.pop();
if (i == 0) sum = sum + front_node->data;
if (front_node->left) q.push(front_node->left);
if (front_node->right) q.push(front_node->right);
}
}
cout<<sum<<endl;
}
int main(){
Node* root = newNode(12);
root->left = newNode(10);
root->right = newNode(30);
root->right->left = newNode(25);
root->right->right = newNode(40);
sumLeftView(root);
return 0;
}
|
Java
import java.util.*;
class Node {
int data;
Node left, right;
Node( int data) {
this .data = data;
left = right = null ;
}
}
class Main {
static Node newNode( int data) {
return new Node(data);
}
static void sumLeftView(Node root) {
if (root == null )
return ;
Queue<Node> q = new LinkedList<Node>();
q.add(root);
int sum = 0 ;
while (!q.isEmpty()) {
int n = q.size();
for ( int i = 0 ; i < n; i++) {
Node front_node = q.poll();
if (i == 0 )
sum = sum + front_node.data;
if (front_node.left != null )
q.add(front_node.left);
if (front_node.right != null )
q.add(front_node.right);
}
}
System.out.println(sum);
}
public static void main(String[] args) {
Node root = newNode( 12 );
root.left = newNode( 10 );
root.right = newNode( 30 );
root.right.left = newNode( 25 );
root.right.right = newNode( 40 );
sumLeftView(root);
}
}
|
Python3
from queue import Queue
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def sumLeftView(root):
if not root: return
q = Queue()
q.put(root)
sum = 0
while not q.empty():
n = q.qsize()
for i in range (n):
front_node = q.get()
if i = = 0 :
sum + = front_node.data
if front_node.left:
q.put(front_node.left)
if front_node.right:
q.put(front_node.right)
print ( sum )
if __name__ = = '__main__' :
root = Node( 12 )
root.left = Node( 10 )
root.right = Node( 30 )
root.right.left = Node( 25 )
root.right.right = Node( 40 )
sumLeftView(root)
|
Javascript
class Node {
constructor(data) {
this .data = data;
this .left = null ;
this .right = null ;
}
}
function sumLeftView(root) {
if (root === null )
return ;
let q = [];
q.push(root);
let sum = 0;
while (q.length > 0) {
let n = q.length;
for (let i = 0; i < n; i++) {
let front_node = q.shift();
if (i === 0)
sum = sum + front_node.data;
if (front_node.left !== null )
q.push(front_node.left);
if (front_node.right !== null )
q.push(front_node.right);
}
}
console.log(sum);
}
function main() {
let root = new Node(12);
root.left = new Node(10);
root.right = new Node(30);
root.right.left = new Node(25);
root.right.right = new Node(40);
sumLeftView(root);
}
main();
|
C#
using System;
using System.Collections.Generic;
class Node {
public int data;
public Node left, right;
public Node( int data) {
this .data = data;
left = right = null ;
}
}
class GFG {
static Node newNode( int data) {
return new Node(data);
}
static void sumLeftView(Node root) {
if (root == null )
return ;
Queue<Node> q = new Queue<Node>();
q.Enqueue(root);
int sum = 0;
while (q.Count != 0) {
int n = q.Count;
for ( int i = 0; i < n; i++) {
Node front_node = q.Dequeue();
if (i == 0)
sum = sum + front_node.data;
if (front_node.left != null )
q.Enqueue(front_node.left);
if (front_node.right != null )
q.Enqueue(front_node.right);
}
}
Console.WriteLine(sum);
}
public static void Main( string [] args) {
Node root = newNode(12);
root.left = newNode(10);
root.right = newNode(30);
root.right.left = newNode(25);
root.right.right = newNode(40);
sumLeftView(root);
}
}
|
Time Complexity: O(N) where N is the number of nodes in given binary tree.
Auxiliary Space: O(N) due to queue data structure.
Last Updated :
25 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...