![JAR search and dependency download from the Maven repository](/logo.png)
g0101_0200.s0112_path_sum.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java17 Show documentation
Show all versions of leetcode-in-java17 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0101_0200.s0112_path_sum;
// #Easy #Depth_First_Search #Tree #Binary_Tree
import com_github_leetcode.TreeNode;
public class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if (root == null) {
return false;
}
if (sum == root.val && root.left == null && root.right == null) {
return true;
}
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy