Skip to content

Here is my repo which consists of all the daily leetcode solution 1 Jan 2024 onwards.

Notifications You must be signed in to change notification settings

SomyaJhaa/Daily-Leetcode-Challenge

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Leetcode Daily Challenge Solutions

This is my attempt to make the coding experience easier for you guys so that you can easily learn what to do in today's leetcode challenge.

Always here to assist you guys.

Today's 14-04-24

Intuition

The goal is to find the sum of all left leaves in a binary tree. A left leaf is a leaf node that is the left child of its parent.

Approach

  • I started with the root node.
  • If the current node is null, returned 0.
  • Check if the left child of the current node exists and if it is a leaf node (i.e., it has no left or right children).
  • If the left child is a leaf node, add its value to the result and continue recursively with the right child.
  • If the left child is not a leaf node, recursively call the function on both the left and right children.
  • Returned the sum obtained from the left subtree and the right subtree.

Have a look at the code , still have any confusion then please let me know in the comments Keep Solving.:)

Complexity

  • Time complexity : $O(n)$

$n$ : number of nodes in the binary tree

  • Space complexity : $O(n)$

Code

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */

class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
        if (root == null) {
            return 0;
        }
		
		// Checking if the left node is a leaf node
        if (root.left != null && root.left.left == null && root.left.right == null) {
            return root.left.val + sumOfLeftLeaves(root.right);
        }

		// Exploring the tree further
        return sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right);
    }
}