Given the root of a binary tree, return the sum of values of its deepest leaves.
&nbsp;
Example 1:

Input: root = [1,2,3,4,5,null,6,7,null,null,null,null,8]
Output: 15


Example 2:

Input: root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5]
Output: 19


&nbsp;
Constraints:


	The number of nodes in the tree is in the range [1, 104].
	1 &lt;= Node.val &lt;= 100


# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    
    """
    THOUGHT PROCESS:
        1) find the height of the tree, this way we would know how deep we need to go.
        2) we need a counter to check how deep we are and this is not available in deepestLeavesSum so we create a new function deepestLeave.
        3) now we go in depth, if we are at bottom, we return the value, we recursively visit both left and right nodes.
        
    """
    
    def height(self, root):
        if root is None:
            return 0
        else:
            x, y = 1, 1
            if root.left:
                x = self.height(root.left)+1
            if root.right:
                y = self.height(root.right)+1
            
            return max(x, y)
            
    
    def deepestLeave(self, root, depth):
        
        if root is None:
            return 0
        
        if root.left is None and root.right is None:
            if depth == 1:
                return root.val
        
        return self.deepestLeave(root.left, depth-1) + self.deepestLeave(root.right, depth-1)
    
    def deepestLeavesSum(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        return self.deepestLeave(root, self.height(root))
        
        
        
        
        
class Solution {
    public int height(TreeNode root)
    {
        if(root == null)
            return 0;
        return Math.max(height(root.left), height(root.right)) + 1;
    }
    public int deepestLeavesSum(TreeNode root) {
        if(root == null) return 0;
        if(root.left == null && root.right == null) return root.val;
        Queue<TreeNode> q = new LinkedList<>();
        q.add(root);
        q.add(null);
        int hght = height(root);
        int sum = 0;
        while(q.size()>0 && q.peek()!=null)
        {
            TreeNode temp = q.remove();
            if(temp.left!=null) q.add(temp.left);
            if(temp.right!=null) q.add(temp.right);
            if(q.peek() == null)
            {
                q.remove();
                q.add(null);
                hght--;
            }
            if(hght == 1)
            {
                while(q.size()>0 && q.peek()!=null)
                {
                    sum+=q.remove().val;
                }
            }

        }
        return sum;
    }
}
						**	#Vote if u like ❤️**
							
	class Solution {
	public:
		void fun(TreeNode *root , int l , map<int , vector<int>> &m)
		{
			if(root == NULL) return; //If root is NULL it will return 
			m[l].push_back(root -> val); //With level as key inserting the value to the vector
			fun(root -> left, l + 1, m); // passing the left with level + 1 
			fun(root -> right, l + 1 , m); // passing right with level + 1
		}
		int deepestLeavesSum(TreeNode* root) {
			map<int , vector<int>> m;  // Map with key as level and value of vector for storing the values
			fun(root , 0 , m); // A void fun with map and level as 0
		   auto itr = m.rbegin(); //Hence we need the leaves node the nodes are present in last level 
			int sum = 0; // Sum Variable 
			for(int i = 0; i < itr-> second.size(); i++) {sum += itr -> second[i];}
			return sum;
		}
	};
	
														-Yash:)
var deepestLeavesSum = function(root) {
    let queue = [];
    queue.push([root, 0]);
    let sum = 0;
    let curLevel = 0
    let i = 0;
    while(queue.length-i > 0) {
        let [node, level] = queue[i];
        queue[i] = null;
        i++;
        if(level > curLevel) {
            sum = 0;
            curLevel = level;
        }
        sum += node.val;
        if(node.left != null) queue.push([node.left, level+1]);
        if(node.right != null) queue.push([node.right, level+1]);
    }
    return sum;
    //time: o(n)
    //space: o(n)
};
Deepest Leaves Sum