Class Solution

java.lang.Object
g0101_0200.s0112_path_sum.Solution

public class Solution extends Object
112 - Path Sum.<p>Easy</p> <p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <code>true</code> if the tree has a <strong>root-to-leaf</strong> path such that adding up all the values along the path equals <code>targetSum</code>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg" alt="" /></p> <p><strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22</p> <p><strong>Output:</strong> true</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" alt="" /></p> <p><strong>Input:</strong> root = [1,2,3], targetSum = 5</p> <p><strong>Output:</strong> false</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> root = [1,2], targetSum = 0</p> <p><strong>Output:</strong> false</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li> <li><code>-1000 <= Node.val <= 1000</code></li> <li><code>-1000 <= targetSum <= 1000</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • hasPathSum

      public boolean hasPathSum(TreeNode root, int sum)