Day 16: Path Sum (Tree Depth First Search)

Day 16: Path Sum (Tree Depth First Search)

Path Sum

Recommended: Solution

Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum.

A leaf is a node with no children.


Leetcode


Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1]
targetSum = 22
Output: true

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right

def hasPathSum(root, targetSum):
    if not root:
        return False
    
   
    if not root.left and not root.right:
        return targetSum == root.val
    
    return hasPathSum(root.left, targetSum - root.val) or hasPathSum(root.right, targetSum

        

  • Time Complexity: O(n), where n is the number of nodes in the tree.
  • Space Complexity: O(h), where h is the height of the tree

Kosisochukwu Mbachu-Igwe

Data Analyst | Public Health & Patient Support Specialist Enhancing Healthcare Outcomes with Data-Driven Insights | Customer Analytics & Predictive Insights | Machine Learning for Business Growth |

6 个月

Great tips. Keep it up ??

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

Olaoluwa Saola的更多文章

  • Amazon Redshift Data Warehouse Project Using S3, Boto3, and psycopg2

    Amazon Redshift Data Warehouse Project Using S3, Boto3, and psycopg2

    In this project, I worked with Amazon Redshift and S3 to build a data warehouse solution, automating data ingestion and…

    2 条评论
  • Data Warehouse part 2: OLAP vs OLTP

    Data Warehouse part 2: OLAP vs OLTP

    Data engineering plays a pivotal role in modern data management, enabling organizations to efficiently process and…

    2 条评论
  • Building a Data Warehouse Part 1: A Step-by-Step Guide

    Building a Data Warehouse Part 1: A Step-by-Step Guide

    In today’s data-driven world, the ability to effectively store, manage, and analyze large volumes of data is crucial…

    8 条评论
  • Data Engineering 101: Setting Up PostgreSQL with Python

    Data Engineering 101: Setting Up PostgreSQL with Python

    Read article on Medium Data engineering involves managing and processing data for analysis, which often starts with…

    4 条评论
  • Day 20: Find Median of a Datastream (Two Heaps)

    Day 20: Find Median of a Datastream (Two Heaps)

    Median of a Datastream Recommended: Solution The median is the middle value in an ordered integer list. If the size of…

  • Day 19: Modified Binary Search (Search Insert Position)

    Day 19: Modified Binary Search (Search Insert Position)

    Search Insert Position Recommended: Solution Given a sorted array of distinct integers and a target value, return the…

  • Day 18 Missing Number (Cyclic Sort)

    Day 18 Missing Number (Cyclic Sort)

    Missing Number Recommended: Solution Given an array containing distinct numbers in the range , return the only number…

    2 条评论
  • Day 17: Path Sum II (DFS)

    Day 17: Path Sum II (DFS)

    Path Sum II Recommended: Solution Given the of a binary tree and an integer , return all root-to-leaf paths where the…

  • Recap

    Recap

    ?? Here are the links to my previous posts: Day 15: Zig zag Binary Tree Traversal Day 14: Reverse Level order traversal…

    4 条评论
  • Day 15: Zig Zag Binary Tree Traversal (BFS)

    Day 15: Zig Zag Binary Tree Traversal (BFS)

    Zig Zag Binary Tree Traversal Recommended:Solution Time and space complexities : O(n) Leetcode

    2 条评论

社区洞察

其他会员也浏览了