[JAVA] DFS approach || Deepest Leaves Sum || Beginner approach
class Solution {

	public int deepestLeavesSum(TreeNode root) {
    
    int ans = finddepth(root);
   
    int res = findans(root,ans);
    
    return res;
}

int sum = 0;

public int findans(TreeNode root,int ans)
{
    
    if(root==null)
    {
        return sum;
    }
    
    if(ans==1)
    {
        sum=sum+(root.val);
    }
    
    
    findans(root.left,ans-1);
    findans(root.right,ans-1);
    
    return sum;
         
}

public int finddepth(TreeNode root)
{
    if(root==null)
    {
        return 0;
    }
    
    int left = finddepth(root.left);
    int right = finddepth(root.right);
    
    return 1 + Math.max(left,right);
}
}
Comments (0)