Given the root node of a tree, find the sum of all leaf nodes that are deepest from the root node.

**example:**

1 / \ 2 3 / \ / \ 4 5 6 7Some:root (of the upper tree)exit:22explain:Nodes with maximum depth are: 4, 5, 6, 7. So the sum of these nodes is = 22

Suggestion: try your approach on {IDE} before proceeding with the solution.

When traversing nodes, compare the node's level to max_level (the maximum level up to the current node). If the current level exceeds the max level, update max_level to the current level. If the maximum and current levels are the same, add the historical data to the current total. If the level is below max_level, do nothing.

**implement:**

## C++

`/ / Get the code of the node sum`

`// exists at maximum depth.`

`#Include`

`use`

`Immensky-Raum`

`standard;`

`integer`

`sum = 0, max_level = INT_MIN;`

`structured`

`node`

`{`

`integer`

`D;`

`Çvor *l;`

`Çvor *r;`

`};`

`// function that returns a new node`

`node * create node(`

`integer`

`D）`

`{`

`node * node;`

`node =`

`new`

`node;`

`node->d = d;`

`node->l = NULL;`

`node->r = NULL;`

`return`

`node;`

`}`

`// function to get node sum`

`// exists at maximum depth.`

`// Compare levels when traversing nodes`

`// node with max_level`

`// (the maximum level of the current node).`

`// If the current level exceeds the maximum level,`

`// Update max_level to current level.`

`// If the maximum level is equal to the current level,`

`// Add master data to the current total.`

`Cancel`

`sumOfNodesAtMaxDepth(node *ro,`

`integer`

`Eben)`

`{`

`I`

`(ro == NULL)`

`return`

`;`

`I`

`(Ebene > max_level)`

`{`

`sum = ro -> d;`

`max_level = razina;`

`}`

`others`

`I`

`(level == max level)`

`{`

`sum = sum + ro -> d;`

`}`

`sumOfNodesAtMaxDepth(ro -> l, level + 1);`

`sumOfNodesAtMaxDepth(ro -> r, Ebene + 1);`

`}`

`// driver code`

`integer`

`main()`

`{`

`node * root;`

`root = createNode(1);`

`root->l = createNode(2);`

`root->r = createNode(3);`

`root->l->l = createNode(4);`

`root->l->r = createNode(5);`

`root->r->l = createNode(6);`

`root->r->r = createNode(7);`

`sumOfNodesAtMaxDepth(root, 0);`

`output << sum;`

`return`

`0;`

`}`

## java

`// Get the Java code for the node sum`

`// exists at maximum depth.`

`class`

`FRP`

`{`

`stationary`

`integer`

`sum =`

`0`

`, max_level = Integer.MIN_VALUE;`

`stationary`

`class`

`node`

`{`

`integer`

`D;`

`Çvor l;`

`Çvor r;`

`};`

`// function that returns a new node`

`stationary`

`node create node(`

`integer`

`D）`

`{`

`knot knot`

`node =`

`new`

`node();`

`node.d = d;`

`node.l =`

`invalid`

`;`

`node.r =`

`invalid`

`;`

`return`

`node;`

`}`

`// function to get node sum`

`// exists at maximum depth.`

`// Compare levels when traversing nodes`

`// node with max_level`

`// (the maximum level of the current node).`

`// If the current level exceeds the maximum level,`

`// Update max_level to current level.`

`// If the maximum level is equal to the current level,`

`// Add master data to the current total.`

`stationary`

`Cancel`

`sumOfNodesAtMaxDepth(node ro,`

`integer`

`Eben)`

`{`

`I`

`(ro ==`

`invalid`

`)`

`return`

`;`

`I`

`(Ebene > max_level)`

`{`

`sum = ro. D;`

`max_level = razina;`

`}`

`others`

`I`

`(level == max level)`

`{`

`sum = sum + ro. D;`

`}`

`sumOfNodesAtMaxDepth(ro.l, Ebene +`

`1`

`);`

`sumOfNodesAtMaxDepth(ro . r, Ebene +`

`1`

`);`

`}`

`// driver code`

`people`

`stationary`

`Cancel`

`main (String[]-Argumente)`

`{`

`root node;`

`root = createnode(`

`1`

`);`

`root.l = create node(`

`2`

`);`

`root.r = create node(`

`3`

`);`

`root.l.l = createNode(`

`4`

`);`

`root.l.r = createNode(`

`5`

`);`

`root.r.l = createNode(`

`6`

`);`

`root.r.r = createNode(`

`7`

`);`

`sumOfNodesAtMaxDepth(root,`

`0`

`);`

`System.out.println(sum);`

`}`

`}`

`/* This code was created by PrinciRaj1992 */`

## Python3

`# Python3 code to find node sum`

`# Exists at maximum depth.`

`crowd`

`=`

`[`

`0`

`]`

`maximales level`

`=`

`[`

`-`

`(`

`2`

`*`

`*`

`32`

`)]`

`# binary tree node`

`class`

`Create a node:`

`definition`

`__you give me__(`

`yourself`

`, data):`

`yourself`

`.D`

`=`

`data`

`yourself`

`.l`

`=`

`No`

`yourself`

`.R`

`=`

`No`

`# Function to find node sum`

`# Exists at maximum depth.`

`# compare levels as you pass through nodes`

`the number of nodes with max_level`

`# (maximum level of the current node).`

`# If the current level exceeds the maximum level,`

`# Update max_level to current level.`

`# If the maximum level is the same as the current level,`

`# Add master data to current totals.`

`definition`

`sumOfNodesAtMaxDepth(ro, level):`

`I`

`(Ro`

`=`

`=`

`No`

`):`

`return`

`I`

`(level > max_level[`

`0`

`]):`

`crowd`

`[`

`0`

`]`

`=`

`Ro. Man`

`Maximales level[`

`0`

`]`

`=`

`Eben`

`Elf`

`(Eben`

`=`

`=`

`Maximales level[`

`0`

`]):`

`crowd`

`[`

`0`

`]`

`=`

`crowd`

`[`

`0`

`]`

`+`

`Ro. Man`

`sumOfNodesAtMaxDepth(ro.l, level`

`+`

`1`

`)`

`sumOfNodesAtMaxDepth(ro . r, level`

`+`

`1`

`)`

`# on the driver's door`

`root`

`=`

`create node (`

`1`

`)`

`root.l`

`=`

`create node (`

`2`

`)`

`root. correct`

`=`

`create node (`

`3`

`)`

`root.l.l`

`=`

`create node (`

`4`

`)`

`root.l.r`

`=`

`create node (`

`5`

`)`

`root.r.l`

`=`

`create node (`

`6`

`)`

`root.r.r`

`=`

`create node (`

`7`

`)`

`sumOfNodesAtMaxDepth(root,`

`0`

`)`

`Print`

`(`

`crowd`

`[`

`0`

`])`

`# This code was created by SHUBHAMSINGH10`

## C＃

`// C# code to get the node and`

`// exists at maximum depth.`

`use`

`system;`

`class`

`FRP`

`{`

`stationary`

`integer`

`sum = 0, max_level =`

`integer`

`.MinValue;`

`people`

`class`

`node`

`{`

`people`

`integer`

`D;`

`people`

`Çvor l;`

`people`

`Çvor r;`

`};`

`// function that returns a new node`

`stationary`

`node create node(`

`integer`

`D）`

`{`

`knot knot`

`node =`

`new`

`node();`

`node.d = d;`

`node.l =`

`invalid`

`;`

`node.r =`

`invalid`

`;`

`return`

`node;`

`}`

`// function to get node sum`

`// exists at maximum depth.`

`// Compare levels when traversing nodes`

`// node with max_level`

`// (the maximum level of the current node).`

`// If the current level exceeds the maximum level,`

`// Update max_level to current level.`

`// If the maximum level is equal to the current level,`

`// Add master data to the current total.`

`stationary`

`Cancel`

`sumOfNodesAtMaxDepth(node ro,`

`integer`

`Eben)`

`{`

`I`

`(ro ==`

`invalid`

`)`

`return`

`;`

`I`

`(Ebene > max_level)`

`{`

`sum = ro. D;`

`max_level = razina;`

`}`

`others`

`I`

`(level == max level)`

`{`

`sum = sum + ro. D;`

`}`

`sumOfNodesAtMaxDepth(ro .l, level + 1);`

`sumOfNodesAtMaxDepth(ro . r, level + 1);`

`}`

`// driver code`

`people`

`stationary`

`Cancel`

`main(String[] params)`

`{`

`root node;`

`root = createNode(1);`

`root.l = createNode(2);`

`root.r = createNode(3);`

`root.l.l = createNode(4);`

`root.l.r = createNode(5);`

`root.r.l = createNode(6);`

`root.r.r = createNode(7);`

`sumOfNodesAtMaxDepth(root, 0);`

`console.WriteLine(sum);`

`}`

`}`

`// This code was contributed by Princi Singh`

## Javascript

`// Javascript code to get node sum`

`// exists at maximum depth.`

`he is`

`sum = 0, max_level = -1000000000;`

`class node`

`{`

`Constructor()`

`{`

`this is here`

`.d = 0;`

`this is here`

`.l =`

`invalid`

`;`

`this is here`

`.r =`

`invalid`

`;`

`}`

`};`

`// function that returns a new node`

`Function`

`create node (d)`

`{`

`he is`

`node;`

`node =`

`new`

`node();`

`node.d = d;`

`node.l =`

`invalid`

`;`

`node.r =`

`invalid`

`;`

`return`

`node;`

`}`

`// function to get node sum`

`// exists at maximum depth.`

`// Compare levels when traversing nodes`

`// node with max_level`

`// (the maximum level of the current node).`

`// If the current level exceeds the maximum level,`

`// Update max_level to current level.`

`// If the maximum level is equal to the current level,`

`// Add master data to the current total.`

`Function`

`sumOfNodesAtMaxDepth(ro, level)`

`{`

`I`

`(ro ==`

`invalid`

`)`

`return`

`;`

`I`

`(Ebene > max_level)`

`{`

`sum = ro. D;`

`max_level = razina;`

`}`

`others`

`I`

`(level == max level)`

`{`

`sum = sum + ro. D;`

`}`

`sumOfNodesAtMaxDepth(ro.l, level + 1);`

`sumOfNodesAtMaxDepth(ro.r, level + 1);`

`}`

`// driver code`

`he is`

`root;`

`root = createNode(1);`

`root.l = createNode(2);`

`root.r = createNode(3);`

`root.l.l = createNode(4);`

`root.l.r = createNode(5);`

`root.r.l = createNode(6);`

`root.r.r = createNode(7);`

`sumOfNodesAtMaxDepth(root, 0);`

`document.write(sum);`

`// This code was created by rrrtnx`

**exit**

22

**Complexity analysis:**

**Time complexity: O(n),**Because we only visit each node once.**Hilfslam: O(h),**Here h is the height of the tree, the extra space is used for the recursive call stack.

This article was written by Ashwin Loganathan. If you like GeeksforGeeks and want to contribute, you can also write an article at write.geeksforgeeks.org or email your article to review-team@geeksforgeeks.org. See how your articles can appear on the GeeksforGeeks homepage and help other geeks.

**Right to use:**Computes the maximum depth for a given tree. Now start traversing the tree, as we did when calculating the maximum depth. But this time with another parameter (e.g. max depth) where depth is recursively decremented by 1 each time left or right is called. where max == 1, which means the maximum depth node has been reached. So add the data value to the sum. Finally, return the sum.

Here is the implementation of the above method:

## C++

`// C++ code for node sum`

`// at max depth`

`#Include`

`use`

`Immensky-Raum`

`standard;`

`structured`

`node`

`{`

`integer`

`data;`

`node *left, *right;`

`// constructor`

`node(`

`integer`

`data)`

`{`

`this is here`

`-> data = data;`

`this is here`

`-> link = null;`

`this is here`

`-> right = empty;`

`}`

`};`

`// Function to find sum of nodes`

`// The maximum depth parameter is node i`

`// max, where max is the depth`

`// node each time node if is called`

`// the maximum value will be equal to 1, so`

`// We are at the lowest node.`

`integer`

`sumMaxLevelRec(vor* vor,`

`integer`

`maximum)`

`{`

`// basic situation`

`I`

`（vor == NULL）`

`return`

`0;`

`// max == 1 track node`

`// at the deepest level`

`I`

`(max == 1)`

`return`

`node -> data;`

`// Recursively call the left and right nodes`

`return`

`sumMaxLevelRec(node->left, max - 1) +`

`sumMaxLevelRec(pre->setpoint, max - 1);`

`}`

`// function to find maximum depth`

`// the maximum depth of the tree`

`integer`

`max_depth(node*node)`

`{`

`// basic situation`

`I`

`（vor == NULL）`

`return`

`0;`

`// is left depth or right depth`

`// taller, add 1 to include height`

`//Called node`

`return`

`1 + max(maxDepth(node->left),`

`max_depth(node->right));`

`}`

`integer`

`sumMaxLevel (before *number)`

`{`

`// call the function to calculate`

`// maximum depth`

`integer`

`max_depth = max_depth(root);`

`return`

`sumMaxLevelRec(root, MaxDepth);`

`}`

`// at the driver`

`integer`

`main()`

`{`

`/* 1`

`/ \`

`2 3`

`/ \ / \`

`4 5 6 7 */`

`// build tree`

`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);`

`root->right->left=`

`new`

`node(6);`

`root->right->right=`

`new`

`node(7);`

`// call to calculate the sum needed`

`cout<<(sumMaxLevel(root))<`

`}`

`// This code was contributed by Arnab Kund`

## java

`// Java code for node sum`

`// at max depth`

`import`

`java.util.*;`

`class`

`node{`

`integer`

`data;`

`left node, right node;`

`// constructor`

`people`

`node(`

`integer`

`data)`

`{`

`this is here`

`.data = data;`

`this is here`

`.left =`

`invalid`

`;`

`this is here`

`.right =`

`invalid`

`;`

`}`

`}`

`class`

`GfG{`

`// Function to find sum of nodes`

`// The maximum depth parameter is node i`

`// max, where max is the depth`

`// node each time node if is called`

`// the maximum value will be equal to 1, so`

`// We are at the lowest node.`

`people`

`stationary`

`integer`

`sumMaxLevelRec(previous,`

`integer`

`maximum)`

`{`

`// basic situation`

`I`

`(node ==`

`invalid`

`)`

`return`

`0`

`;`

`// max == 1 track node`

`// at the deepest level`

`I`

`(make ==`

`1`

`)`

`return`

`node data;`

`// Recursively call the left and right nodes`

`return`

`sumMaxLevelRec(node.left, max -`

`1`

`) +`

`sumMaxLevelRec(node.right, max -`

`1`

`);`

`}`

`people`

`stationary`

`integer`

`sumMaxLevel(Corijen-Knoten) {`

`// call the function to calculate`

`// maximum depth`

`integer`

`max_depth = max_depth(root);`

`return`

`sumMaxLevelRec(root, MaxDepth);`

`}`

`// function to find maximum depth`

`// the maximum depth of the tree`

`people`

`stationary`

`integer`

`Maximum Depth (Nodes)`

`{`

`// basic situation`

`I`

`(node ==`

`invalid`

`)`

`return`

`0`

`;`

`// is left depth or right depth`

`// taller, add 1 to include height`

`//Called node`

`return`

`1`

`+ Math.max(maxDepth(node.left),`

`max_depth(node.right));`

`}`

`// at the driver`

`people`

`stationary`

`Cancel`

`main (String[]-Argumente)`

`{`

`/* 1`

`/ \`

`2 3`

`/ \ / \`

`4 5 6 7 */`

`// build tree`

`Neuten Uzel =`

`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`

`);`

`root.right.left =`

`new`

`node(`

`6`

`);`

`root.right.right =`

`new`

`node(`

`7`

`);`

`// call to calculate the sum needed`

`System.out.println(sumMaxLevel(root));`

`}`

`}`

## Python3

`# Python3 code to sum max depth nodes`

`class`

`node:`

`definition`

`__you give me__(`

`yourself`

`, data):`

`yourself`

`. data`

`=`

`data`

`yourself`

`. Link`

`=`

`No`

`yourself`

`. law`

`=`

`No`

`# Function to find the sum of maximum depth nodes`

`# Parameters are node and max, where Max should match`

`# node depth calls if max for each node`

`# will be equal to 1, which means we are at the deepest node.`

`definition`

`sumMaxLevelRec(vor,`

`Max`

`):`

`# base case`

`I`

`node`

`=`

`=`

`No`

`:`

`return`

`0`

`# Max == 1 to track the deepest node`

`I`

`Max`

`=`

`=`

`1`

`:`

`return`

`node data`

`# Recursively call the left and right nodes`

`return`

`(sumMaxLevelRec(node.left,`

`Max`

`-`

`1`

`)`

`+`

`sumMaxLevelRec(node.right,`

`Max`

`-`

`1`

`))`

`definition`

`sumMaxLevel（korijen）：`

`# Call the function to calculate the maximum depth`

`maximum depth`

`=`

`Maximum Depth (Root)`

`return`

`sumMaxLevelRec(root, max_depth)`

`# Find the maximum depth function`

`# The maximum depth of the tree`

`definition`

`Maximum depth (nodes):`

`# base case`

`I`

`node`

`=`

`=`

`No`

`:`

`return`

`0`

`# Either left depth or right depth`

`# taller, add 1 to include height`

`#Called node`

`return`

`1`

`+`

`maximum`

`(maxDubth(node.left),`

`max-tiefe(node.right))`

`# on the driver's door`

`I`

`__repeat__`

`=`

`=`

`"__main__"`

`:`

`# build a tree`

`root`

`=`

`node(`

`1`

`)`

`root. Link`

`=`

`node(`

`2`

`)`

`root. correct`

`=`

`node(`

`3`

`)`

`root.left.left`

`=`

`node(`

`4`

`)`

`root.left.right`

`=`

`node(`

`5`

`)`

`root.right.left`

`=`

`node(`

`6`

`)`

`root. correct. correct`

`=`

`node(`

`7`

`)`

`# Call to calculate the required amount`

`Print`

`(sumMaxLevel(root))`

`# This code was contributed by Rituraj Jain`

## C＃

`use`

`system;`

`// C# code for the sum node`

`// at max depth`

`people`

`class`

`node`

`{`

`people`

`integer`

`data;`

`people`

`left node, right node;`

`// constructor`

`people`

`node(`

`integer`

`data)`

`{`

`this is here`

`.data = data;`

`this is here`

`.left =`

`invalid`

`;`

`this is here`

`.right =`

`invalid`

`;`

`}`

`}`

`people`

`class`

`GFG`

`{`

`// Function to find sum of nodes`

`// The maximum depth parameter is node i`

`// max, where max is the depth`

`// node each time node if is called`

`// the maximum value will be equal to 1, so`

`// We are at the lowest node.`

`people`

`stationary`

`integer`

`sumMaxLevelRec(previous,`

`integer`

`maximum)`

`{`

`// basic situation`

`I`

`(node ==`

`invalid`

`)`

`{`

`return`

`0;`

`}`

`// max == 1 track node`

`// at the deepest level`

`I`

`(max == 1)`

`{`

`return`

`node data;`

`}`

`// Recursively call the left and right nodes`

`return`

`sumMaxLevelRec(node.left, max - 1)`

`+ sumMaxLevelRec(node.right, max - 1);`

`}`

`people`

`stationary`

`integer`

`sumMaxLevel(Corijen-Knoten)`

`{`

`// call the function to calculate`

`// maximum depth`

`integer`

`max_depth = max_depth(root);`

`return`

`sumMaxLevelRec(root, MaxDepth);`

`}`

`// function to find maximum depth`

`// the maximum depth of the tree`

`people`

`stationary`

`integer`

`Maximum Depth (Nodes)`

`{`

`// basic situation`

`I`

`(node ==`

`invalid`

`)`

`{`

`return`

`0;`

`}`

`// is left depth or right depth`

`// taller, add 1 to include height`

`//Called node`

`return`

`1 + Math.Max(maxDepth(node.left),`

`max_depth(node.right));`

`}`

`// at the driver`

`people`

`stationary`

`Cancel`

`mainly(`

`Serie A`

`[] parameter)`

`{`

`/* 1`

`/ \`

`2 3`

`/ \ / \`

`4 5 6 7 */`

`// build tree`

`Neuten Uzel =`

`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);`

`root.right.left =`

`new`

`node(6);`

`root.right.right =`

`new`

`node(7);`

`// call to calculate the sum needed`

`Console.WriteLine(sumMaxLevel(root));`

`}`

`}`

`// This code was contributed by Shrikant13`

## Javascript

`// javascript code to sum max depth nodes`

`// Tree node structure.`

`class node`

`{`

`constructor(data) {`

`this is here`

`.left =`

`invalid`

`;`

`this is here`

`.right =`

`invalid`

`;`

`this is here`

`.data = data;`

`}`

`}`

`// Function to find sum of nodes`

`// The maximum depth parameter is node i`

`// max, where max is the depth`

`// node each time node if is called`

`// the maximum value will be equal to 1, so`

`// We are at the lowest node.`

`Function`

`sumMaxLevelRec(vor, max)`

`{`

`// basic situation`

`I`

`(node ==`

`invalid`

`)`

`return`

`0;`

`// max == 1 track node`

`// at the deepest level`

`I`

`(max == 1)`

`return`

`node data;`

`// Recursively call the left and right nodes`

`return`

`sumMaxLevelRec(node.left, max - 1) +`

`sumMaxLevelRec(node.right, max - 1);`

`}`

`Function`

`sumMaxLevel(root) {`

`// call the function to calculate`

`// maximum depth`

`let MaxDepth = maxDepth(root);`

`return`

`sumMaxLevelRec(root, MaxDepth);`

`}`

`// function to find maximum depth`

`// the maximum depth of the tree`

`Function`

`Maximum Depth (knots)`

`{`

`// basic situation`

`I`

`(node ==`

`invalid`

`)`

`return`

`0;`

`// is left depth or right depth`

`// taller, add 1 to include height`

`//Called node`

`return`

`1 + Math.max(maxDepth(node.left),`

`max_depth(node.right));`

`}`

`/* 1`

`/ \`

`2 3`

`/ \ / \`

`4 5 6 7 */`

`// build tree`

`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);`

`root.right.left =`

`new`

`node(6);`

`root.right.right =`

`new`

`node(7);`

`// call to calculate the sum needed`

`dokument.pisati (sumMaxLevel (korijen));`

`// This code was contributed by divyesh072019.`

**exit**

22

**Complexity analysis:**

**Time complexity: O(N),**where N is the number of nodes in the tree.**Adjacent room:****oh)**, where h is the height of the tree, and the extra space is used for the recursive call stack.

**Method three:**use queue

**Right to use:**In this approach the idea is to traverse the tree in order of levels and for each level calculate the sum of all nodes at that level. For each level, we push all the nodes of the tree into a row and calculate the sum of the nodes. So when we get to the end leaf of the tree, the sum is the sum of all the leaves in the binary tree.

Here is the implementation of the above method:

## C++

`// C++ code for the above method`

`#Include`

`use`

`Immensky-Raum`

`standard;`

`structured`

`tree node`

`{`

`integer`

`value;`

`tree node *left;`

`tree node * pair;`

`};`

`// function that returns a new node`

`tree node * create node (`

`integer`

`D）`

`{`

`tree node * node;`

`node =`

`new`

`tree node();`

`node->value = d;`

`node->left = empty;`

`node->right = empty;`

`return`

`node;`

`}`

`// iterate function to find the lowest sum`

`// node.`

`integer`

`Deepest Leaf Sum (TreeNode *root)`

`{`

`// returns 0 if root is NULL`

`I`

`(root == NULL)`

`{`

`return`

`0;`

`}`

`// Initialize an empty queue.`

`represent`

`// root of queue tree`

`Qutui(root);`

`// Initialize the sum of the current level to 0`

`integer`

`sumOfCurrLevel = 0;`

`// Loop until the queue is empty`

`although`

`(!qu.empty())`

`{`

`integer`

`size = qu.size();`

`sumOfCurrLevel = 0;`

`although`

`(size --> 0)`

`{`

`TreeNode *head = qu.front();`

`song.pop();`

`sumOfCurrLevel += head->val;`

`// if the left child of the head is not NULL`

`I`

`(head->left != NULL)`

`{`

`//put the child into the queue`

`qu.push(head->left);`

`}`

`// if true child is not NULL`

`I`

`(head->right != NULL)`

`{`

`// put the child in the queue`

`qu.push(head->right);`

`}`

`}`

`}`

`return`

`sum of current levels;`

`}`

`// at the driver`

`integer`

`main()`

`{`

`tree node *root;`

`root = createNode(1);`

`root->left = createNode(2);`

`root->desno = createNode(3);`

`root->left->left = createNode(4);`

`root->left->right = createNode(5);`

`root->right->left = createNode(6);`

`root->right->right = createNode(7);`

`cout << (minimum sum of leaves (roots));`

`return`

`0;`

`}`

`// This code was contributed by Potta Lokesh`

## java

`// Java code to print the largest existing worksheet sum`

`// the depth of the binary tree`

`import`

`java.util.*;`

`class`

`FRP {`

`stationary`

`class`

`tree node {`

`integer`

`value;`

`tree node links;`

`the tree node on the right;`

`};`

`// function that returns a new node`

`stationary`

`The tree node creates a node (`

`integer`

`D）`

`{`

`node tree node;`

`node =`

`new`

`tree node();`

`node.val = d;`

`node.left =`

`invalid`

`;`

`node.right =`

`invalid`

`;`

`return`

`node;`

`}`

`// iterate function to find the lowest sum`

`// node.`

`people`

`stationary`

`integer`

`deepest leaf sum (root of tree node)`

`{`

`// returns 0 if root is null`

`I`

`(root ==`

`invalid`

`) {`

`return`

`0`

`;`

`}`

`// Initialize an empty queue.`

`rot`

`new`

`linked list<>();`

`// root of queue tree`

`qu.offer(root);`

`// Initialize the sum of the current level to 0`

`integer`

`current level sum =`

`0`

`;`

`// Loop until the queue is empty`

`although`

`(!qu.isEmpty()) {`

`integer`

`size = qu.size();`

`sumOfCurrLevel =`

`0`

`;`

`although`

`(size -->`

`0`

`) {`

`TreeNode head = qu.poll();`

`sumOfCurrLevel += head.val;`

`// if head's left child is not zero`

`I`

`(head.left!=`

`invalid`

`) {`

`//put the child into the queue`

`qu.offer(head.left);`

`}`

`// if the real child is not null`

`I`

`(head.right!=`

`invalid`

`) {`

`// put the child in the queue`

`qu.offer(head.right);`

`}`

`}`

`}`

`return`

`sum of current levels;`

`}`

`people`

`stationary`

`Cancel`

`main (String[]-Argumente)`

`{`

`root tree node;`

`root = createnode(`

`1`

`);`

`root.left = createNode(`

`2`

`);`

`root.right = createNode(`

`3`

`);`

`root.left.left = createNode(`

`4`

`);`

`root.left.right = createNode(`

`5`

`);`

`root.right.left = createNode(`

`6`

`);`

`root.right.right = createNode(`

`7`

`);`

`System.out.println(deepestLeavesSum(root));`

`}`

`}`

`// This code was contributed by Rohan Raj`

## Python3

`#Python code to print the sum`

`the maximum number of pages that exist`

`# The depth of the binary tree`

`class`

`Tree node:`

`definition`

`__you give me__(`

`yourself`

`):`

`yourself`

`.val`

`=`

`0`

`yourself`

`. Link`

`=`

`No`

`yourself`

`. law`

`=`

`No`

`definition`

`Create node (d):`

`node`

`=`

`tree node()`

`node.val`

`=`

`Man`

`node. left`

`=`

`No`

`node. correct`

`=`

`No`

`return`

`node`

`# Iterative function to find the minimum sum`

`number of nodes.`

`definition`

`Minimum leaf sum (root):`

`# If root is None, return 0`

`I`

`(root`

`=`

`=`

`No`

`):`

`return`

`0`

`# Initialize an empty queue.`

`and`

`=`

`[]`

`# Add the root of the tree to the queue`

`qu.add(root)`

`# Initialize the sum of the current level to 0`

`sum of current levels`

`=`

`0`

`# Loop until the queue is empty`

`although`

`(`

`lens`

`(song)!`

`=`

`0`

`):`

`size`

`=`

`lens`

`(and)`

`sum of current levels`

`=`

`0`

`although`

`(size):`

`Kopf`

`=`

`this [`

`0`

`]`

`and`

`=`

`this [`

`1`

`:]`

`sum of current levels`

`+`

`=`

`head. the waves`

`# If the left child of the head is None`

`I`

`(Head. Left!`

`=`

`No`

`):`

`# add the child to the queue`

`qu.append(head.left)`

`# if real child is None`

`I`

`(Head. Yes!`

`=`

`No`

`):`

`# add the child to the queue`

`qu.add(head.right)`

`size`

`-`

`=`

`1`

`return`

`sum of current levels`

`root`

`=`

`create node (`

`1`

`)`

`root. Link`

`=`

`create node (`

`2`

`)`

`root. correct`

`=`

`create node (`

`3`

`)`

`root.left.left`

`=`

`create node (`

`4`

`)`

`root.left.right`

`=`

`create node (`

`5`

`)`

`root.right.left`

`=`

`create node (`

`6`

`)`

`root. correct. correct`

`=`

`create node (`

`7`

`)`

`Print`

`(lowest leaf sum (root))`

`# This code is provided by Shinjanpatra`

## C＃

`// C# code to print the maximum total number of worksheets`

`// the depth of the binary tree`

`use`

`system;`

`use`

`system.collection.generic;`

`people`

`class`

`FRP {`

`people`

`class`

`tree node {`

`people`

`integer`

`value;`

`people`

`tree node links;`

`people`

`the tree node on the right;`

`};`

`// function that returns a new node`

`stationary`

`The tree node creates a node (`

`integer`

`D）`

`{`

`node tree node;`

`node =`

`new`

`tree node();`

`node.val = d;`

`node.left =`

`invalid`

`;`

`node.right =`

`invalid`

`;`

`return`

`node;`

`}`

`// iterate function to find the lowest sum`

`// node.`

`people`

`stationary`

`integer`

`deepest leaf sum (root of tree node)`

`{`

`// returns 0 if root is null`

`I`

`(root ==`

`invalid`

`)`

`{`

`return`

`0;`

`}`

`// Initialize an empty queue.`

`rot`

`new`

`rot`

`// root of queue tree`

`qu.Enqueue(corows);`

`// Initialize the sum of the current level to 0`

`integer`

`sumOfCurrLevel= 0;`

`// Loop until the queue is empty`

`although`

`(qu.Count != 0)`

`{`

`integer`

`size = qu.Count;`

`sumOfCurrLevel = 0;`

`although`

`(size --> 0)`

`{`

`TreeNode head = qu.Dequeue();`

`sumOfCurrLevel += head.val;`

`// if head's left child is not zero`

`I`

`(head.left!=`

`invalid`

`)`

`{`

`//put the child into the queue`

`qu.Enqueue(head.left);`

`}`

`// if the real child is not null`

`I`

`(head.right!=`

`invalid`

`)`

`{`

`// put the child in the queue`

`qu. Enqueue (glava.desno);`

`}`

`}`

`}`

`return`

`sum of current levels;`

`}`

`people`

`stationary`

`Cancel`

`main(String[] params)`

`{`

`root tree node;`

`root = createNode(1);`

`root.left = createNode(2);`

`root.right = createNode(3);`

`root.left.left = createNode(4);`

`root.left.right = createNode(5);`

`root.right.left = createNode(6);`

`root.right.right = createNode(7);`

`Console.WriteLine(deepestLeavesSum(root));`

`}`

`}`

`// This code was contributed by umadevi9616`

## Javascript

`// JavaScript code to print the sum`

`//The maximum number of worksheets that exist`

`// the depth of the binary tree`

`class tree node`

`{`

`Constructor()`

`{`

`this is here`

`.val=0;`

`this is here`

`.left=`

`this is here`

`.right=`

`invalid`

`;`

`}`

`}`

`Function`

`create node (d)`

`{`

`Untie the knot;`

`node =`

`new`

`tree node();`

`node.val = d;`

`node.left =`

`invalid`

`;`

`node.right =`

`invalid`

`;`

`return`

`node;`

`}`

`// iterate function to find the lowest sum`

`// node.`

`Function`

`lowest leaf sum (root)`

`{`

`// returns 0 if root is null`

`I`

`(root ==`

`invalid`

`) {`

`return`

`0;`

`}`

`// Initialize an empty queue.`

`You are qu=[];`

`// root of queue tree`

`Qutui(root);`

`// Initialize the sum of the current level to 0`

`neka sumOfCurrLevel = 0;`

`// Loop until the queue is empty`

`although`

`(qu.length!=0) {`

`let size = qu.length;`

`sumOfCurrLevel = 0;`

`although`

`(size --> 0) {`

`let head = qu.shift();`

`sumOfCurrLevel += head.val;`

`// if head's left child is not zero`

`I`

`(head.left!=`

`invalid`

`) {`

`//put the child into the queue`

`qu.push(head.left);`

`}`

`// if the real child is not null`

`I`

`(head.right!=`

`invalid`

`) {`

`// put the child in the queue`

`qu.push(head.right);`

`}`

`}`

`}`

`return`

`sum of current levels;`

`}`

`neka root = createNode(1);`

`root.left = createNode(2);`

`root.right = createNode(3);`

`root.left.left = createNode(4);`

`root.left.right = createNode(5);`

`root.right.left = createNode(6);`

`root.right.right = createNode(7);`

`document.write(deepestLeavesSum(root));`

`// This code was created by avanitrachhadiya2155`

**exit**

22

**Complexity analysis:**

**Time complexity: O(N)**where N is the number of nodes in the tree.**Hilveslam: O(b),**Here b is the width of the tree, the extra space is used to store the current level's elements in the queue.

my personal notes*Arrow_drop_up*

latest update:15. September 2022

as an article

save article

## FAQs

### What is the maximum number of nodes possible in a binary tree of depth? ›

1) The maximum number of nodes at level 'l' of a binary tree is **2 ^{l}^{-}^{1}**. Here level is number of nodes on path from root to the node (including root and node). Level of root is 1.

**How to calculate the maximum number of nodes in a binary tree with depth k? ›**

The maximum number of nodes in a binary tree of depth k is **2k−1, k≥1**. Here the depth of the tree is 1. So according to the formula, it will be 21−1=1.

**What is the maximum number of nodes in a binary tree with depth 3? ›**

Answer: A perfect binary tree of height 3 has 2^{3}^{+}^{1} – 1 = **15 nodes**.

**What is the maximum number of nodes in a binary tree of depth 6? ›**

A symmetric binary tree with depth n (n=6) has the maximum number of nodes for n = 6 is , i.e. 64 - 1 = 63. As a result, a symmetric binary tree with a depth of 6 can have up to **63 nodes**.

**What is the minimum number of nodes in a binary tree of depth? ›**

Minimum number of nodes in a binary tree whose height is h. **At least one node at each of first h levels**. All possible nodes at first h levels are present. A full binary tree of a given height h has 2h – 1 nodes.

**How do you find the depth of all nodes in a binary tree? ›**

The depth of a node in a binary tree is **the total number of edges from the root node to the target node**. Similarly, the depth of a binary tree is the total number of edges from the root node to the most distant leaf node.

**What is the maximum depth of a binary tree space complexity? ›**

The maximum depth is **the number of nodes along the longest path from the root node down to the farthest leaf node**. return its depth = 3. Algorithm: In order to find the maximum depth of the binary tree as a whole, at each node one has to determine which among its left and right subtrees have a greater depth.

**How do you calculate the number of nodes in a binary tree? ›**

We can calculate the minimum number of nodes with level n in a binary tree with the help of formula: **n - 1**. The behavior of linked list data structure and the binary tree is the same.

**How many number of nodes will be there if maximum height of binary tree is n? ›**

Concept: In a binary tree, a node can have maximum two children. If there are n nodes in binary tree, maximum height of the binary tree is **n-1**.

**What is the maximum number of nodes that each child have in binary tree? ›**

What is the maximum number of children that a binary tree node can have? Explanation: In a binary tree, a node can have atmost **2 nodes** (i.e.) 0,1 or 2 left and right child. 2.

### How many maximum child nodes can a node in a binary tree have? ›

A binary tree has a special condition that each node can have **two children at maximum**. “In computer science, a binary tree is a tree data structure in which each node has at the most two children, which are referred to as the left child and the right child.”

**What is the depth of a node in a tree? ›**

The depth of a node is **the number of edges from that node to the tree's root node**. As such, the depth of the whole tree would be the depth of its deepest leaf node. The root node has a depth of 0.

**What is the maximum number of nodes that can be found in a binary tree at level 3 4 and 12? ›**

In this level, the binary tree has maximum **8 nodes** which are the child of level 2 nodes. Where: r is the level number.

**What is the average depth of a binary tree? ›**

The average depth of a node in a randomly constructed binary search tree is **O(log n)**.

**What is the minimum and maximum number of nodes in a binary search tree? ›**

**2h+1 – 1 is the maximum number of nodes in a full binary tree of height "h." 2h+1 is the minimum number of nodes in a full binary tree of height "h."**

**What is the deepest node in a binary tree? ›**

Deepest node in a tree

**The rightmost node among the leaf nodes** is known as the deepest node in tree. To find the deepest node in a binary tree we can traverse through all the the nodes in a tree and return the rightmost node among the leaf nodes.

**How many nodes are there in a complete binary tree of depth 5? ›**

The maximum number of binary trees of height 5 is =2^{(}^{5}^{+}^{1}^{)}-1=2^{6}-1 = **63 nodes**.

**How do you find the max depth decision tree? ›**

We can set the maximum depth of our decision tree **using themax_depth parameter**. The more the value of max_depth, the more complex your tree will be. The training error will off-course decrease if we increase the max_depth value but when our test data comes into the picture, we will get a very bad accuracy.

**How do you find the maximum depth or height of a binary tree? ›**

The height or maximum depth of a binary tree is **the total number of edges on the longest path from the root node to the leaf node**. In other words, the height of a binary tree is equal to the maximum number of edges from the root to the most distant leaf node.

**What is the maximum number of levels that a binary search tree with 100 nodes can have? ›**

Maximum number of levels: For a binary search tree to have the maximum number of levels, it must be the most unbalanced tree possible. This occurs when each node has only one child, essentially forming a linked list. In this case, the tree has 100 nodes, so the maximum number of levels is **100**. 2.

### How many binary trees have 4 nodes? ›

Enumerating Binary Trees

There is one binary tree with one node. There are two differently shaped trees with two nodes. There are **14** different (shaped) binary trees with four nodes.

**What is the maximum height of a tree with 32 nodes? ›**

Similarly, a complete tree of height 5 has to have at least 32 nodes, but can have **up to 63**. Non-complete trees?

**What is the minimum number of nodes in a binary tree of height 5? ›**

**A binary tree's height cannot be bigger than the number of nodes or vertices in the tree**. So yes, the minimum number of vertices required for a binary tree of height 5 will be 5.

**What is the maximum number of children that a node can have in a binary tree * 3 1 4 2? ›**

The Binary tree means that the node can have maximum **two children**.

**Can a tree have 3 child nodes? ›**

In computer science, **a ternary tree is a tree data structure in which each node has at most three child nodes**, usually distinguished as "left", “mid” and "right".

**What is meant by depth of a node? ›**

The depth of a node is **the number of edges from the root to the node**. The height of a node is the number of edges from the node to the deepest leaf. The height of a tree is a height of the root.

**What do you mean by depth of tree? ›**

Depth. In a tree, **many edges from the root node to the particular node** are called the depth of the tree. In the tree, the total number of edges from the root node to the leaf node in the longest path is known as "Depth of Tree".

**What is depth of any tree? ›**

Tree: **Number of edges/arc from the root node to the leaf node of the tree** is called as the Depth of the Tree.

**How do you calculate the average depth of a tree? ›**

The definition of average depth of a tree is **the sum of the depth of all nodes divided by the total number of nodes**.

**What is depth first in binary tree? ›**

Depth-first search, as opposed to breadth first search, of a binary tree is **a form of search that goes from the root to height of the tree before backtracking and repeating to paths that have yet to bee explored**. Consider pushing values of the following binary search tree as nodes are traversed depth-first.

### What is the number of nodes of a full binary tree with k levels? ›

Theorem: Let T be a binary tree. For every k ≥ 0, there are **no more than 2k nodes** in level k. Theorem: Let T be a binary tree with λ levels. Then T has no more than 2λ – 1 nodes.

**What is the maximum number of vertices in k level binary tree? ›**

The maximum number of vertices at level k of a binary tree is **2k , k≥0** (see Exercise 10.4. 6 of this section). A full binary tree is a tree for which each vertex has either zero or two empty subtrees. In other words, each vertex has either two or zero children.

**What is the maximum number of nodes in a k ary tree of height h? ›**

The maximum number of nodes in a binary tree of height h is **2^(h+1) - 1**. This is known as the full binary tree property.

**What is the number of nodes in the k tree? ›**

For any k-ary tree the total number of nodes **n = [(k^(h+1))-1]/(h-1)** where h is the height of the k-ary tree. Ex:- For complete binary tree(k=2) total no. of nodes = [(2^(h+1))-1]/(h-1). So for height 3 the total no.

**What is the minimum and maximum number of nodes in a binary tree? ›**

Complete binary tree:

**The maximum number of nodes in a complete binary tree is 2h+1 -1**. The minimum number of nodes in a complete binary tree is 2h. The minimum height of a complete binary tree is log2(n+1) – 1.

**What is the maximum number of nodes in a binary tree of height 8? ›**

I imagine you are talking about a binary tree, so the answer is easy. 2^k-1, so in your case **2^8-1 = 255**. Take a tree of height k, if you add a new level you can duplicate the number of nodes by adding two nodes in each leaf.

**What is the maximum in a binary tree? ›**

The maximum depth of a binary tree is **the number of nodes from the root down to the furthest leaf node**. In other words, it is the height of a binary tree. The maximum depth, or height, of this tree is 4; node 7 and node 8 are both four nodes away from the root.

**What is the maximum number of edges in a binary tree? ›**

Since a binary tree can contain at most one node at level 0 (the root), it can contain at most 2l node at level l. 4. The total number of edges in a full binary tree with n node is **n - 1**.

**What is the maximum number of nodes in a decision tree? ›**

The maximum number of terminal nodes in a tree is **2 to the power of the depth**.

**How many trees can be made from 5 nodes? ›**

Therefore, the total number of possible binary trees with 5 nodes is 4 + 3 + 1 = **8**. Hence, the correct answer is option D (42).

### What is the number of nodes at depth d in a k ary tree? ›

A complete d-ary tree of depth k has dℓ on level ℓ (where 0≤ℓ≤k), so its total number of nodes is **k∑ℓ=0dℓ=dk+1−1d−1**.