Sum of Left Leaves(Day14)

Sum of Left Leaves(Day14)

Problem statement:

Sum of Left Leaves

A leaf is a node with no children. A left leaf is a leaf that is the left child of another node.

Example 1:

Input: root = [3,9,20,null,null,15,7]
Output: 24
Explanation: There are two left leaves in the binary tree, with values 9 and 15 respectively.        

Intuition:

we have two traversal methods DFS and BFS, and here we will use preorder traversal.

  1. 1. Start at the root of the binary tree.
  2. 2. Check if the current node has a left child:

a. If it does, recursively visit the left child by calling the function with true indicating it's a left call.

b. If the left child is a leaf node (i.e., both its left and right children are null), return true.

3. If the current node is a right call (i.e., passed false), return false to indicate that it's not a perfect left-leaf node.

4. Check if the current node has a right child: a. If it does, recursively visit the right child by calling the function with false indicating it's a right call. b. If the right child is a left-leaf node, return true.

5. If none of the above conditions are met, return false.

6. Repeat this process for all nodes in the tree until all left-leaf nodes are found.

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;
        }
        return getSum(root,false);
    }
    public int getSum(TreeNode root, boolean isLeftChild){
        if(root.left==null && root.right==null){
            return (isLeftChild)?root.val :0;
        }
        int sum=0;
        if(root.left!=null){
            sum += getSum(root.left, true);
        }
        if(root.right!=null){
            sum += getSum(root.right, false);
        }
        return sum;
    }
}        

Time complexity: O(N) because we are traversing each node.

Space complexity: O(N) using recursion stack space.

要查看或添加评论,请登录

Sakshi Nayak的更多文章

  • Remove K Digits (Day 11)

    Remove K Digits (Day 11)

    Problem Statement Remove K Digits Given string num representing a non-negative integer num, and an integer k, return…

  • Time Needed to Buy Tickets(Day 9)

    Time Needed to Buy Tickets(Day 9)

    Problem Statement: Time Needed to Buy Tickets There are people in a line queuing to buy tickets, where the person is at…

  • Number of Students Unable to Eat Lunch(Day 8)

    Number of Students Unable to Eat Lunch(Day 8)

    Problem Statement: Read the Problem Statement in below link provided. Number of Students Unable to Eat Lunch You are…

  • Valid Parenthesis String(Day 7)

    Valid Parenthesis String(Day 7)

    Problem Statement: Valid Parenthesis String Given a string containing only three types of characters: , and , return if…

  • Make The String Great(Day 5)

    Make The String Great(Day 5)

    Problem statement: Make The String Great Given a string of lower and upper case English letters. A good string is a…

  • Maximum Nesting Depth of the Parentheses(Day 4)

    Maximum Nesting Depth of the Parentheses(Day 4)

    Problem Statement: Maximum Nesting Depth of the Parentheses A string is a valid parentheses string (denoted VPS) if it…

  • Word Search(Day 3)

    Word Search(Day 3)

    Problem Statement: 79. Word Search Given an grid of characters and a string , return if exists in the grid.

  • Isomorphic Strings (Day 2)

    Isomorphic Strings (Day 2)

    Problem statement: 205. Isomorphic Strings Given two strings and , determine if they are isomorphic.

  • 58. Length of last word(Day 1)

    58. Length of last word(Day 1)

    Day1: Problem - 58. Length of Last Word (Easy) Problem statement Given a string consisting of words and spaces, return…