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. ...
随机推荐
- 查看php 相关信息
PHP系统常量 __FILE__ 当前PHP程序脚本的绝对路径及文件名称 __LINE__ 存储该常量所在的行号 __FUNCTION__ 存储该常量所在的函数名称 __CLASS__ 存储该常量所在 ...
- asp:FileUpload 控件上传多文件
<asp:FileUpload runat="server" ID="imgUpload" AllowMultiple="true" ...
- 两个数据库通过DataTable实现差异传输
两个主要方法 /// <summary>/// 用途:/// 用源表和目标表比较,返回差异的数据(目标表为参照物)/// /// 逻辑:/// 1.合并两个表/// 2.循环合并后得到的表 ...
- Vuejs 实现权限管理
程序运行时,router只配置登陆 首页404 等基本页面 import Main from '@/views/Main.vue'; // 不作为Main组件的子页面展示的页面单独写,如下 expor ...
- git 的搭建与使用
公司之前用的是vpn,然后老大说让我搞一个git.于是,我开始了git的研究之路.... 概念:(说实话,看了还是有些不太理解) git 是一种版本控制系统,是一个命令,是一种工具 g ...
- 数据结构---Java---有序数组
[自定义有序数组] 查找算法: 线性查找算法:依次对比查询: 二分查找算法:必须是有序: insert:要插入的value与数组中每个元素进行比较,当有值>value时,此处的index之后的元 ...
- spring事务的传播性
<!--配置事务传播特性 --><tx:advice id = "txAdvice" transaction-manager = "txManage&q ...
- sql server 2017安装
下载: 1. 2. 3. 安装步骤: https://www.cnblogs.com/ksguai/p/5869558.html 管理工具: Microsoft SQL Server Manageme ...
- 《我在谷歌大脑见习机器学习的一年:Node.js创始人的尝试笔记》阅读笔记
文章来源:https://www.toutiao.com/i6539751003690893828/?tt_from=weixin_moments&utm_campaign=client_sh ...
- stm32串口学习(二)
今天继续学习stm32的串口编程(利用库函数).上次我们说了串口的发送,这次我们说接收. 接收可以用查询的方法,也可以用中断.显然,工程中多用中断的方式,那么就来看看中断接收. 代码其实很简单,基本的 ...