View on GitHub

leetcode-diaries

A journal to record the solution of the various Leetcode problems solved over the course of time.

Path Sum

Leetcode problem 112; Difficulty: Easy

Problem Statement

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.

Constraints:

Example 1:

image

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

Output: true

Example 2:

image

Input: root = [1,2,3], targetSum = 5

Output: false

Example 3:

Input: root = [1,2], targetSum = 0

Output: false

Submitted Solution

The implemented solution received the following rating on Leetcode:

image