[LeetCode]题解(python):112 Path Sum
题目来源
https://leetcode.com/problems/path-sum/
Given a binary tree and a 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.
题意分析
Input: a binary tree, sum
Output: True or False
Conditions:判断是否存在一条路径,从root-leaf的路径,使得路径的value的和等于sum
题目思路
递归遍历,通过一个不断递减的sum值去判断。
AC代码(Python)
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def hasPathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: bool
"""
if root == None:
return False
if root.left == None and root.right == None:
return root.val == sum
return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum - root.val)
[LeetCode]题解(python):112 Path Sum的更多相关文章
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III
112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...
- Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划)
Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划) 题目描述 已知一个正方形二维数组A,我们想找到一条最小下降路径的和 所谓下降路径是指,从一行到 ...
- [LeetCode] 112. Path Sum 二叉树的路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [LeetCode] 112. Path Sum 路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [LeetCode] 112. Path Sum ☆(二叉树是否有一条路径的sum等于给定的数)
Path Sum leetcode java 描述 Given a binary tree and a sum, determine if the tree has a root-to-leaf pa ...
- [leetcode]Binary Tree Maximum Path Sum @ Python
原题地址:https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ 题意: Given a binary tree, find th ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode 931. Minimum Falling Path Sum
原题链接在这里:https://leetcode.com/problems/minimum-falling-path-sum/ 题目: Given a square array of integers ...
- [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
随机推荐
- BZOJ3838 : [Pa2013]Raper
将选取的$A$看成左括号,$B$看成右括号,那么答案是一个合法的括号序列. 那么只要重复取出$k$对价值最小的左右括号,保证每时每刻都是一个合法的括号序列即可. 将$($看成$1$,$)$看成$-1$ ...
- c/c++ 关于swap的不同写法
利用模板 template<typename T>inline void swap(T& a, T& b){ T tmp = a; a = b; b = tmp; } 利用 ...
- BestCoder Round #78 (div.2)
因为rating不够QAQ就报了Div2.. [CA Loves Stick] CA喜欢玩木棍. 有一天他获得了四根木棍,他想知道用这些木棍能不能拼成一个四边形. Sample Input 2 1 1 ...
- FileStorage Read String Start With Number Need Quotation Mark 读取数字开头的字符串需要加引号
// Write data FileStorage fs("test.yml", FileStorage::WRITE); fs << "MyString&q ...
- Add Customerlize Button in More Button List In Odoo
There're two commen type of actions in odoo: ir.actions.server,ir.actions.client_multi 1.Using ir.ac ...
- php 类
<?php class mysql{ private $host; private $name; private $pass; private $table; private $ut; func ...
- Apache开启不了------The requested operation has failed!
1.这个问题通常是由于端口被占用了,cmd输入netstat -ano查看端口号,根据端口号80的程序的pid在任务管理器下杀掉它. 2.我遇到这个问题不是这个原因.我由于刚配置了虚拟目录.查看虚拟目 ...
- 【新产品发布】iM_VGA 真彩显示VGA模块!(含视频教程)
============================== 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:h ...
- POJ 1062 dij
一道读题读的不明所以的题... 每个人只能接受和自己等级差距不超过m的人进行交易 包括间接交易 所以我们可以枚举每一个长度为m的围绕着酋长的等级区间 每次都对这个等级区间内的人进行操作 求dis[1] ...
- POJ 2299 树状数组+离散化求逆序对
给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...