Remove distracting text, extract the programming question, provide simple, common, and optimized Java solution for it.

Problem List
0
Debugging...
Run
Submit
00:00:00
Description
Editorial
Editorial
Solutions
Solutions
Submissions
Submissions
Code
Note
Note
Testcase
Testcase
Test Result
112. Path Sum
Solved
Easy
Topics
Companies

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.

 

Example 1:

Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
Output: true
Explanation: The root-to-leaf path with the target sum is shown.


Example 2:

Input: root = [1,2,3], targetSum = 5
Output: false
Explanation: There are two root-to-leaf paths in the tree:
(1 --> 2): The sum is 3.
(1 --> 3): The sum is 4.
There is no root-to-leaf path with sum = 5.


Example 3:

Input: root = [], targetSum = 0
Output: false
Explanation: Since the tree is empty, there are no root-to-leaf paths.


 

Constraints:

The number of nodes in the tree is in the range [0, 5000].
-1000 <= Node.val <= 1000
-1000 <= targetSum <= 1000
Seen this question in a real interview before?
1/5
Yes
No
Accepted
1.6M
Submissions
3.1M
Acceptance Rate
51.6%
Topics
Tree
Depth-First Search
Breadth-First Search
Binary Tree
Companies
0 - 3 months
Amazon
2
0 - 6 months
Microsoft
4
Google
3
Meta
2
6 months ago
Bloomberg
7
Adobe
4
Apple
2
TikTok
2
Uber
2
Yahoo
2
Similar Questions
Path Sum II
Medium
Binary Tree Maximum Path Sum
Hard
Sum Root to Leaf Numbers
Medium
Path Sum III
Medium
Path Sum IV
Medium
Discussion (94)
Choose a type
Preview
Comment
💡 Discussion Rules

1. Please don't post any solutions in this discussion.

2. The problem discussion is for asking questions about the problem or for sharing tips - anything except for solutions.

3. If you'd like to share your solution for feedback and ideas, please head to the solutions tab and post it there.

Sort by:Best
No comments yet.
1
2
3
4
10
Copyright ©️ 2024 LeetCode All rights reserved
9.9K
94
Java
Auto
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 *     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 boolean hasPathSum(TreeNode root, int targetSum) {
        
    }
}
Saved
Ln 15, Col 4
You must run your code first

Minimum Depth of Binary Tree - LeetCode

Problem List
Tag
38. Count and Say

Med.

39. Combination Sum

Med.

40. Combination Sum II

Med.

41. First Missing Positive

Hard

42. Trapping Rain Water

Hard

43. Multiply Strings

Med.

44. Wildcard Matching

Hard

45. Jump Game II

Med.

46. Permutations

Med.

47. Permutations II

Med.

48. Rotate Image

Med.

49. Group Anagrams

Med.

50. Pow(x, n)

Med.

51. N-Queens

Hard

52. N-Queens II

Hard

53. Maximum Subarray

Med.

54. Spiral Matrix

Med.

55. Jump Game

Med.

56. Merge Intervals

Med.

57. Insert Interval

Med.

58. Length of Last Word

Easy

59. Spiral Matrix II

Med.

60. Permutation Sequence

Hard

61. Rotate List

Med.

62. Unique Paths

Med.

63. Unique Paths II

Med.

64. Minimum Path Sum

Med.

65. Valid Number

Hard

66. Plus One

Easy

67. Add Binary

Easy

68. Text Justification

Hard

69. Sqrt(x)

Easy

70. Climbing Stairs

Easy

71. Simplify Path

Med.

72. Edit Distance

Med.

73. Set Matrix Zeroes

Med.

74. Search a 2D Matrix

Med.

75. Sort Colors

Med.

76. Minimum Window Substring

Hard

77. Combinations

Med.

78. Subsets

Med.

79. Word Search

Med.

80. Remove Duplicates from Sorted Array II

Med.

81. Search in Rotated Sorted Array II

Med.

82. Remove Duplicates from Sorted List II

Med.

83. Remove Duplicates from Sorted List

Easy

84. Largest Rectangle in Histogram

Hard

85. Maximal Rectangle

Hard

86. Partition List

Med.

87. Scramble String

Hard

88. Merge Sorted Array

Easy

89. Gray Code

Med.

90. Subsets II

Med.

91. Decode Ways

Med.

92. Reverse Linked List II

Med.

93. Restore IP Addresses

Med.

94. Binary Tree Inorder Traversal

Easy

95. Unique Binary Search Trees II

Med.

96. Unique Binary Search Trees

Med.

97. Interleaving String

Med.

98. Validate Binary Search Tree

Med.

99. Recover Binary Search Tree

Med.

100. Same Tree

Easy

101. Symmetric Tree

Easy

102. Binary Tree Level Order Traversal

Med.

103. Binary Tree Zigzag Level Order Traversal

Med.

104. Maximum Depth of Binary Tree

Easy

105. Construct Binary Tree from Preorder and Inorder Traversal

Med.

106. Construct Binary Tree from Inorder and Postorder Traversal

Med.

107. Binary Tree Level Order Traversal II

Med.

108. Convert Sorted Array to Binary Search Tree

Easy

109. Convert Sorted List to Binary Search Tree

Med.

110. Balanced Binary Tree

Easy

111. Minimum Depth of Binary Tree

Easy

112. Path Sum

Easy

113. Path Sum II

Med.

114. Flatten Binary Tree to Linked List

Med.

115. Distinct Subsequences

Hard

116. Populating Next Right Pointers in Each Node

Med.

117. Populating Next Right Pointers in Each Node II

Med.

118. Pascal's Triangle

Easy

119. Pascal's Triangle II

Easy

120. Triangle

Med.

121. Best Time to Buy and Sell Stock

Easy

122. Best Time to Buy and Sell Stock II

Med.

123. Best Time to Buy and Sell Stock III

Hard

124. Binary Tree Maximum Path Sum

Hard

125. Valid Palindrome

Easy

126. Word Ladder II

Hard

127. Word Ladder

Hard

128. Longest Consecutive Sequence

Med.

129. Sum Root to Leaf Numbers

Med.

130. Surrounded Regions

Med.

131. Palindrome Partitioning

Med.

132. Palindrome Partitioning II

Hard

133. Clone Graph

Med.

134. Gas Station

Med.

135. Candy

Hard

136. Single Number

Easy

137. Single Number II

Med.

138. Copy List with Random Pointer

Med.

139. Word Break

Med.

140. Word Break II

Hard

141. Linked List Cycle

Easy

142. Linked List Cycle II

Med.

143. Reorder List

Med.

144. Binary Tree Preorder Traversal

Easy

145. Binary Tree Postorder Traversal

Easy

146. LRU Cache

Med.

147. Insertion Sort List

Med.

148. Sort List

Med.

149. Max Points on a Line

Hard

150. Evaluate Reverse Polish Notation

Med.

151. Reverse Words in a String

Med.

152. Maximum Product Subarray

Med.

153. Find Minimum in Rotated Sorted Array

Med.

154. Find Minimum in Rotated Sorted Array II

Hard

155. Min Stack

Med.

156. Binary Tree Upside Down

Med.

157. Read N Characters Given Read4

Easy

158. Read N Characters Given read4 II - Call Multiple Times

Hard

159. Longest Substring with At Most Two Distinct Characters

Med.

160. Intersection of Two Linked Lists

Easy

161. One Edit Distance

Med.

162. Find Peak Element

Med.

163. Missing Ranges

Easy

164. Maximum Gap

Med.

165. Compare Version Numbers

Med.

166. Fraction to Recurring Decimal

Med.

167. Two Sum II - Input Array Is Sorted

Med.

168. Excel Sheet Column Title

Easy

169. Majority Element

Easy

170. Two Sum III - Data structure design

Easy

171. Excel Sheet Column Number

Easy

172. Factorial Trailing Zeroes

Med.

173. Binary Search Tree Iterator

Med.

174. Dungeon Game

Hard

175. Combine Two Tables

Easy

176. Second Highest Salary

Med.

177. Nth Highest Salary

Med.

178. Rank Scores

Med.

179. Largest Number

Med.

180. Consecutive Numbers

Med.

181. Employees Earning More Than Their Managers

Easy

182. Duplicate Emails

Easy

183. Customers Who Never Order

Easy

184. Department Highest Salary

Med.

185. Department Top Three Salaries

Hard

186. Reverse Words in a String II

Med.

187. Repeated DNA Sequences

Med.