LeetCode Path Sum 判断树的路径之和
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool hasPathSum(TreeNode *root, int sum) {
if(root==) //针对根结点,非根节点不应会执行到这里
return false; if( root->left== && root->right== ) //是叶子结点,且刚好将sum变为0
return root->val==sum ? true : false ; if( ( root->left!= && hasPathSum( root->left , sum-root->val) ) || ( root->right!= && hasPathSum( root->right , sum-root->val ) ) )//判断左叉或右叉中是否有一条从上往下满足要求的路径
return true; return false;
}
};
这次终于感觉代码够简洁的了。哈哈
题意是:有没有一条这样一条路径,从根开始到叶子结点上的值之和为所提供的数字。
本来挺不愿意用递归的,递归很有局限性,但用起来又特别爽。像此题,想半个小时没想到怎么设计算法会快一点。如果有非递归算法,且是较好的代码,请不吝分享一下吧!
解题需考虑的是:
1.根结点为空
2.递归到叶子结点了,要设计其作为递归出口
3.非叶子结点要解决sum的问题。只要左子树或者右子树中有一条路径满足要求,那么就判断结束。
LeetCode Path Sum 判断树的路径之和的更多相关文章
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Path Sum IV 二叉树的路径和之四
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- LeetCode:Path Sum I II
LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...
- [LeetCode] Path Sum II 二叉树路径之和之二
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [leetcode] Path sum路径之和
要求给定树,与路径和,判断是否存在从跟到叶子之和为给定值的路径.比如下图中,给定路径之和为22,存在路径<5,4,11,2>,因此返回true;否则返回false. 5 / \ 4 8 / ...
- [LeetCode] 666. Path Sum IV 二叉树的路径和 IV
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- [LeetCode] 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] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- [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. ...
随机推荐
- window.open 设置高和宽无效
当设置_self属性时,再设置宽和高就不管用,这个宽高会继承父窗口的宽高! window.open("url","_self","width=100, ...
- C/C++中qsort()以及sort()的用法
最近学弟们问快速排序的比较多,今天自己就做一下总结,快速排序在库函数里面有现成的,不用自己实现,调用一下就可以达到自己想要的结果,掌握以后就可以完全摒弃冒泡和选择了,并且时间复杂度也从O(n*n)提升 ...
- POJ 2229 Sumsets(规律)
这是一道意想不到的规律题............或许是我比较菜,找不到把. Description Farmer John commanded his cows to search for diffe ...
- java线程类图
Executors创建不同类型的Executor, executor执行不同的runnable task Executor: Runnable:
- js apply和call
apply()和call()这两个方法的作用是一样的,都是在特定作用域中调用函数,等于设置函数体内this对象的只,以扩充函数赖以运行的作用域 apply:方法能劫持另外一个对象的方法,继承另外一个对 ...
- Maven使用之packing篇
项目的打包类型:pom.jar.war 项目中一般使用maven进行模块管理,每个模块下对应都有一个pom文件,pom文件中维护了各模块之间的依赖和继承关系.项目模块化可以将通用的部分抽离出来,方便重 ...
- Python查看类或Module的版本号
>>> import keras >>> print keras.__version__ 1.2.0
- python_文件操作代码实例
"""提示:代码中的内容均被注释,请参考,切勿照搬""" 1 #文件的打开和关闭 ''' 文件对象 = open('文件名','使用方式') ...
- Python函数调用
1.同级目录下的调用 - src |- mod1.py |- test1.py 若在程序test1.py中导入模块mod1.py,则直接使用 import mod1 或 from mod1 impor ...
- RestTemplate中几种常见的请求方式
GET请求 第一种:getForEntity getForEntity方法的返回值是一个ResponseEntity<T>,ResponseEntity<T>是Spring对H ...