Sum of nodes in the left view of the given binary tree
Last Updated : 10 Oct, 2024
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:
Example 1 : The Green colored nodes represents the left view in the below Binary tree. The Left-View will contain the nodes (1, 2, 4), sum of these nodes is 1 + 2 + 4 = 7
Example 2: The Green colored nodes represents the left view in the below Binary tree. The Left-View will contain the nodes (1, 2, 4, 5), sum of these nodes is 1 + 2 + 4 + 5 = 12

[Expected Approach 1] Using Recursion – O(n) Time and O(h) Space
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++ // C++ program to find sum of left view of Binary Tree // using recursion #include <bits/stdc++.h> using namespace std; class Node { public: int data; Node *left, *right; Node(int x) { data = x; left = right = nullptr; } }; // Helper function for the left view sum using Recursion void RecLeftView(Node* root, int level, int& maxLevel, int& sum) { if (!root) { return; } // If current level is more than max level, // this is the first node of that level if (level > maxLevel) { sum += root->data; maxLevel = level; } // Traverse left subtree first, then right subtree RecLeftView(root->left, level + 1, maxLevel, sum); RecLeftView(root->right, level + 1, maxLevel, sum); } // Function to return the sum of nodes in the // left view of the binary tree int leftViewSum(Node *root) { int sum = 0; int maxLevel = -1; // Start recursion with root at level 0 RecLeftView(root, 0, maxLevel, sum); return sum; } int main() { // Representation of the input tree: // 1 // / \ // 2 3 // / \ // 4 5 Node *root = new Node(1); root->left = new Node(2); root->right = new Node(3); root->left->left = new Node(4); root->left->right = new Node(5); int result = leftViewSum(root); cout << result << endl; return 0; }
C // C program to return sum of left view of Binary Tree // using recursion #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node *left, *right; }; // Helper function for the left view sum using Recursion void RecLeftViewSum(struct Node* root, int level, int* maxLevel, int* sum) { if (!root) { return; } // If current level is more than max level, // this is the first node of that level if (level > *maxLevel) { *sum += root->data; *maxLevel = level; } // Traverse left subtree first, then right subtree RecLeftViewSum(root->left, level + 1, maxLevel, sum); RecLeftViewSum(root->right, level + 1, maxLevel, sum); } // Function to return the sum of nodes in the // left view of the binary tree int leftViewSum(struct Node* root) { int sum = 0; int maxLevel = -1; // Start recursion with root at level 0 RecLeftViewSum(root, 0, &maxLevel, &sum); return sum; } struct Node* createNode(int x) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = x; newNode->left = newNode->right = NULL; return newNode; } int main() { // Representation of the input tree: // 1 // / \ // 2 3 // / \ // 4 5 struct Node *root = createNode(1); root->left = createNode(2); root->right = createNode(3); root->left->left = createNode(4); root->left->right = createNode(5); int result = leftViewSum(root); printf("%d\n", result); return 0; }
Java // Java program to find the sum of the left // view of binary tree class Node { int data; Node left, right; Node(int x) { data = x; left = right = null; } } // Helper function for the left view sum using recursion class GfG { static int RecLeftViewSum(Node root, int level, int[] maxLevel) { if (root == null) return 0; int sum = 0; // If current level is more than max level, // this is the first node of that level (left view) if (level > maxLevel[0]) { sum += root.data; maxLevel[0] = level; } // Traverse left subtree first, then right subtree sum += RecLeftViewSum(root.left, level + 1, maxLevel); sum += RecLeftViewSum(root.right, level + 1, maxLevel); return sum; } // Function to return the sum of the // left view of the binary tree static int leftViewSum(Node root) { int[] maxLevel = new int[] {-1}; return RecLeftViewSum(root, 0, maxLevel); } public static void main(String[] args) { // Representation of the input tree: // 1 // / \ // 2 3 // / \ // 4 5 Node root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.left.right = new Node(5); int result = leftViewSum(root); System.out.println(result); } }
Python # Python program to find the sum of the left view of # Binary Tree using Recursion class Node: def __init__(self, data): self.data = data self.left = None self.right = None # Helper function for the left view sum using Recursion def RecLeftViewSum(root, level, maxLevel): if root is None: return 0 sum_left_view = 0 # If current level is more than max level, # this is the first node of that level (left view) if level > maxLevel[0]: sum_left_view += root.data maxLevel[0] = level # Traverse left subtree first, then right subtree sum_left_view += RecLeftViewSum(root.left, level + 1, maxLevel) sum_left_view += RecLeftViewSum(root.right, level + 1, maxLevel) return sum_left_view # Function to return the sum of the left # view of the binary tree def LeftViewSum(root): maxLevel = [-1] return RecLeftViewSum(root, 0, maxLevel) if __name__ == "__main__": # Representation of the input tree: # 1 # / \ # 2 3 # / \ # 4 5 root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) result = LeftViewSum(root) print(result)
C# // C# program to find the sum of the left view of // Binary Tree using Recursion using System; class Node { public int data; public Node left, right; public Node(int x) { data = x; left = right = null; } } class GfG { // Helper function for the left view sum // using Recursion static int RecLeftViewSum(Node root, int level, int[] maxLevel) { if (root == null) { return 0; } int sumLeftView = 0; // If current level is more than max level, // this is the first node of that level (left view) if (level > maxLevel[0]) { sumLeftView += root.data; maxLevel[0] = level; } // Traverse left subtree first, then right subtree sumLeftView += RecLeftViewSum(root.left, level + 1, maxLevel); sumLeftView += RecLeftViewSum(root.right, level + 1, maxLevel); return sumLeftView; } // Function to return the sum of the left // view of the binary tree static int LeftViewSum(Node root) { int[] maxLevel = { -1 }; return RecLeftViewSum(root, 0, maxLevel); } static void Main() { // Representation of the input tree: // 1 // / \ // 2 3 // / \ // 4 5 Node root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.left.right = new Node(5); int result = LeftViewSum(root); Console.WriteLine(result); } }
JavaScript // JavaScript program to find the sum of // the left view of Binary Tree using Recursion class Node { constructor(data) { this.data = data; this.left = null; this.right = null; } } // Helper function for the left view sum // using Recursion function RecLeftViewSum(root, level, maxLevel) { if (root === null) { return 0; } let sumLeftView = 0; // If current level is more than max level, // this is the first node of that level (left view) if (level > maxLevel[0]) { sumLeftView += root.data; maxLevel[0] = level; } // Traverse left subtree first, then right subtree sumLeftView += RecLeftViewSum(root.left, level + 1, maxLevel); sumLeftView += RecLeftViewSum(root.right, level + 1, maxLevel); return sumLeftView; } // Function to return the sum of the left // view of the binary tree function LeftViewSum(root) { let maxLevel = [-1]; return RecLeftViewSum(root, 0, maxLevel); } // Representation of the input tree: // 1 // / \ // 2 3 // / \ // 4 5 let root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.left.right = new Node(5); let result = LeftViewSum(root); console.log(result);
[Expected Approach 2] Using Level Order Traversal – O(n) Time and O(n) Space
The idea is to 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, return the sum.
Below is the implementation of the above approach:
C++ // C++ program to find sum of left view of Binary Tree // using level order traversal #include <bits/stdc++.h> using namespace std; class Node { public: int data; Node *left, *right; Node(int x) { data = x; left = right = nullptr; } }; // Function to return the sum of nodes in the // left view of the binary tree using // level order traversal int leftViewSum(Node *root) { if (!root) { return 0; } int sum = 0; queue<Node*> q; q.push(root); while (!q.empty()) { int levelSize = q.size(); // Iterate through all nodes of the current level for (int i = 0; i < levelSize; ++i) { Node *curr = q.front(); q.pop(); // Add the first node of each // level (left view) if (i == 0) { sum += curr->data; } // Push the left and right children of // the current node if (curr->left) { q.push(curr->left); } if (curr->right) { q.push(curr->right); } } } return sum; } int main() { // Representation of the input tree: // 1 // / \ // 2 3 // / \ // 4 5 Node *root = new Node(1); root->left = new Node(2); root->right = new Node(3); root->left->left = new Node(4); root->left->right = new Node(5); int result = leftViewSum(root); cout << result << endl; return 0; }
Java // Java program to find the sum of the left // view of binary tree import java.util.LinkedList; import java.util.Queue; class Node { int data; Node left, right; Node(int x) { data = x; left = right = null; } } // Function to return the sum of the // left view of the binary tree using // level order traversal class GfG { static int leftViewSum(Node root) { if (root == null) return 0; int sum = 0; Queue<Node> queue = new LinkedList<>(); queue.offer(root); while (!queue.isEmpty()) { int levelSize = queue.size(); // Iterate through all nodes of the // current level for (int i = 0; i < levelSize; i++) { Node curr = queue.poll(); // Add the first node of each level // (left view) if (i == 0) { sum += curr.data; } // Push the left and right children // of the current node if (curr.left != null) { queue.offer(curr.left); } if (curr.right != null) { queue.offer(curr.right); } } } return sum; } public static void main(String[] args) { // Representation of the input tree: // 1 // / \ // 2 3 // / \ // 4 5 Node root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.left.right = new Node(5); int result = leftViewSum(root); System.out.println(result); } }
Python # Python program to find the sum of the left view of # Binary Tree using Level Order Traversal from collections import deque class Node: def __init__(self, data): self.data = data self.left = None self.right = None # Function to return the sum of the left # view of the binary tree def LeftViewSum(root): if root is None: return 0 sum_left_view = 0 queue = deque([root]) while queue: level_size = len(queue) # Iterate through all nodes of the # current level for i in range(level_size): curr = queue.popleft() # Add the first node of each # level (left view) if i == 0: sum_left_view += curr.data # Push the left and right children of # the current node if curr.left: queue.append(curr.left) if curr.right: queue.append(curr.right) return sum_left_view if __name__ == "__main__": # Representation of the input tree: # 1 # / \ # 2 3 # / \ # 4 5 root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) result = LeftViewSum(root) print(result)
C# // C# program to find the sum of the left view of // Binary Tree using Level Order Traversal using System; using System.Collections.Generic; class Node { public int data; public Node left, right; public Node(int x) { data = x; left = right = null; } } class GfG { // Function to return the sum of the left // view of the binary tree static int LeftViewSum(Node root) { if (root == null) return 0; int sumLeftView = 0; Queue<Node> queue = new Queue<Node>(); queue.Enqueue(root); while (queue.Count > 0) { int levelSize = queue.Count; // Iterate through all nodes of the current level for (int i = 0; i < levelSize; i++) { Node curr = queue.Dequeue(); // Add the first node of each level (left view) if (i == 0) { sumLeftView += curr.data; } // Push the left and right children // of the current node if (curr.left != null) { queue.Enqueue(curr.left); } if (curr.right != null) { queue.Enqueue(curr.right); } } } return sumLeftView; } static void Main() { // Representation of the input tree: // 1 // / \ // 2 3 // / \ // 4 5 Node root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.left.right = new Node(5); int result = LeftViewSum(root); Console.WriteLine(result); } }
JavaScript // JavaScript program to find the sum of // the left view of Binary Tree using // Level Order Traversal class Node { constructor(data) { this.data = data; this.left = null; this.right = null; } } // Function to return the sum of the left view // of the binary tree function LeftViewSum(root) { if (root === null) return 0; let sumLeftView = 0; const queue = [root]; while (queue.length > 0) { const levelSize = queue.length; // Iterate through all nodes of the // current level for (let i = 0; i < levelSize; i++) { const curr = queue.shift(); // Add the first node of each // level (left view) if (i === 0) { sumLeftView += curr.data; } // Push the left and right children of // the current node if (curr.left) { queue.push(curr.left); } if (curr.right) { queue.push(curr.right); } } } return sumLeftView; } // Representation of the input tree: // 1 // / \ // 2 3 // / \ // 4 5 let root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.left.right = new Node(5); let result = LeftViewSum(root); console.log(result);
Similar Reads
Sum of nodes in the right 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 right view. The right view of a binary tree is the set of nodes visible when the tree is viewed from the right.Examples: Input: 1 / \ 2 3 / \ \ 4 5 6Output: 101 + 3 + 6 = 10Input: 1 / \ 2 3 \ 4 \ 5 \ 6Output: 19Ap
15+ min read
Sum of nodes in top view of binary tree
Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. Given a binary tree, the task is to print the sum of nodes in top view.Examples: Input: 1 / \ 2 3 / \ \ 4 5 6 Output: 16 Input: 1 / \ 2 3 \ 4 \ 5 \ 6 Output: 12 Approach: The idea is to put nodes of same hori
13 min read
Sum of nodes in bottom view of Binary Tree
Given a binary tree, the task is to return the sum of nodes in the bottom view of the given Binary Tree. The bottom view of a binary tree is the set of nodes visible when the tree is viewed from the bottom. Note: If there are multiple bottom-most nodes for a horizontal distance from the root, then t
15+ min read
Sum of nodes in a binary tree having only the left child nodes
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 2Output: 18 Explanation: Nodes with values 5, 6, and 7 are the ones that have only the left child nodes Input: 2 / \ 3 1 / / 5 6Output: 4 Approach: The
6 min read
Print Nodes in Top View of Binary Tree
Given a binary tree. The task is to find the top view of the binary tree. The top view of a binary tree is the set of nodes visible when the tree is viewed from the top. Note: Return the nodes from the leftmost node to the rightmost node.If two nodes are at the same position (horizontal distance) an
15+ min read
Sum of all leaf nodes of binary tree
Given a binary tree, find the sum of all the leaf nodes.Examples: Input : 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Output : Sum = 4 + 5 + 8 + 7 = 24 Recommended PracticeSum of Leaf NodesTry It! The idea is to traverse the tree in any fashion and check if the node is the leaf node or not. If the node is the lea
5 min read
Find sum of all nodes of the given perfect binary tree
Given a positive integer L which represents the number of levels in a perfect binary tree. Given that the leaf nodes in this perfect binary tree are numbered starting from 1 to n, where n is the number of leaf nodes. And the parent node is the sum of the two child nodes. Our task is to write a progr
11 min read
Sum of cousins of a given node in a Binary Tree
Given a binary tree and data value of a node. The task is to find the sum of cousin nodes of given node. If given node has no cousins then return -1. Note: It is given that all nodes have distinct values and the given node exists in the tree. Examples: Input: 1 / \ 3 7 / \ / \ 6 5 4 13 / / \ 10 17 1
11 min read
Count of nodes with average of left subtree at least K in a given Binary Tree
Given a binary tree and a number K, the task is to count the number of nodes having the average of the values in their left subtree greater than or equal to K. Examples: Input : K=5Tree: 2 / \ 5 4 / \ / \ 5 6 6 2 \ / 5 4Output: 3Explanation: 2 -------- level 0 / \ 5 4 -------- level 1 / \ / \ 5 6 6
15+ min read
Count the nodes in the given tree whose sum of digits of weight is odd
Given a tree, and the weights of all the nodes, the task is to count the number of nodes whose sum of digits of weights is odd.Examples: Input: Output: 3 Node 1: digitSum(144) = 1 + 4 + 4 = 9 Node 2: digitSum(1234) = 1 + 2 + 3 + 4 = 10 Node 3: digitSum(21) = 2 + 1 = 3 Node 4: digitSum(5) = 5 Node 5:
6 min read