112. Path Sum
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to determine if a binary tree contains a "root-to-leaf" path where the sum of all node values along that path equals a given targetSum. A key part of the definition is that a path must end at a leaf node, which is a node with no children. An empty tree has no root-to-leaf paths.
Solution
Path Sum in Python
# 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
class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
# If the root is null, there are no paths, so return False.
if not root:
return False
# Check if the current node is a leaf node.
# A leaf node has no left and no right children.
if not root.left and not root.right:
# If it's a leaf, check if the path sum equals the target sum.
return targetSum == root.val
# Recursively call for the left and right children with the updated target sum.
# The `or` operator provides short-circuiting, which is efficient.
return self.hasPathSum(root.left, targetSum - root.val) or \
self.hasPathSum(root.right, targetSum - root.val)
Path Sum in Java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* 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) {
// If the root is null, there are no paths, so return false.
if (root == null) {
return false;
}
// Subtract the current node's value from the target sum.
targetSum -= root.val;
// Check if the current node is a leaf node.
// A leaf node has no left and no right children.
if (root.left == null && root.right == null) {
// If it's a leaf, check if the remaining sum is zero.
// This means we've found a valid path.
return targetSum == 0;
}
// Recursively call for the left and right children.
// If a path is found in either the left or right subtree, return true.
// The || operator provides short-circuiting, which is efficient.
return hasPathSum(root.left, targetSum) || hasPathSum(root.right, targetSum);
}
}
Path Sum in C++
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool hasPathSum(TreeNode* root, int targetSum) {
// If the root is null, there are no paths, so return false.
if (root == nullptr) {
return false;
}
// Subtract the current node's value from the target sum.
targetSum -= root->val;
// Check if the current node is a leaf node.
// A leaf node has no left and no right children.
if (root->left == nullptr && root->right == nullptr) {
// If it's a leaf, check if the remaining sum is zero.
// This means we've found a valid path.
return targetSum == 0;
}
// Recursively call for the left and right children.
// If a path is found in either the left or right subtree, return true.
// The || operator provides short-circuiting, which is efficient.
return hasPathSum(root->left, targetSum) || hasPathSum(root->right, targetSum);
}
};
Path Sum in TypeScript
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
function hasPathSum(root: TreeNode | null, targetSum: number): boolean {
// If the root is null, there are no paths, so return false.
if (!root) {
return false;
}
// Subtract the current node's value from the target sum.
targetSum -= root.val;
// Check if the current node is a leaf node.
// A leaf node has no left and no right children.
if (root.left === null && root.right === null) {
// If it's a leaf, check if the remaining sum is zero.
// This means we've found a valid path.
return targetSum === 0;
}
// Recursively call for the left and right children.
// If a path is found in either the left or right subtree, return true.
// The || operator provides short-circuiting, which is efficient.
return hasPathSum(root.left, targetSum) || hasPathSum(root.right, targetSum);
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.