# Leetcode Path Sum

Source: Internet
Author: User

Leetcode Problem Solving path sum original problem

Determines whether a binary tree has a path from the root node to a leaf node, and is a specific value for all nodes on that path.

Note the point:

• No

Example:

Input: sum = 12

``    3     9  20    /     15   7  / 14``

Output: True (3->9)

Thinking of solving problems

Directly through the return solution, judge a node, first the value of the required values minus the value of the node, if the remaining required value is 0 and the current node is a leaf node, then the path from the root node to the current node to meet the requirements of the topic. Otherwise, the left and right nodes of the current node will continue to be recursive.

AC Source
``# Definition for a binary tree node. class TreeNode(object):     def __init__(self, x):Self.val = x Self.left =NoneSelf.right =None class solution(object):     def haspathsum(self, root, sum):        "" : Type Root:TreeNode:type sum:int:rtype:bool "" "        if  notRootreturn FalseSum-= Root.valifsum = =0  andRoot.left is None  andRoot.right is None:return True        returnSelf.haspathsum (Root.left, sum)orSelf.haspathsum (root.right, sum)if__name__ = ="__main__":None``

Welcome to my GitHub (Https://github.com/gavinfish/LeetCode-Python) to get the relevant source code.

Leetcode Path Sum

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.