← 返回 meta 的题目列表Path Sum
类型:online_judge
Given a binary tree and a target sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. The structure of a tree node is as follows:
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
Example
Input
root = [5,4,8,11,null,13,4,7,2,null,null,null,1], sum = 22
Output
True
Explanation
The path is 5->4->11->2, which sums to 22.
Constraints
The number of nodes in the tree will not exceed 5000.
-1000 <= Node value <= 1000
Example
Input
[5,4,8,11,null,13,4,7,2,null,null,null,1], 22