[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 the values along the path equals the given sum.
For example:
Given the below binary tree andsum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
return true, as there exist a root-to-leaf path5->4->11->2which sum is 22.
题意:给定一个数,判断是否存在一条从根节点到叶节点的路径,使得,路径上节点所对应的值得和等于这个数。
方法一:使用递归解法。
针对一条路径:通过用sum减去当前节点的值,直到最后看最后叶节点的值是否等于sum剩余的值来判断是否存在。
一、终止条件,1)初始时,root为空,返回false;最后,root->left、root->right不存在时,依旧不等于sum,也返回false;
2)当达到叶节点时,当前节点的值等于sum剩余值,返回true;
二、递归表达式,对一个节点,左、右子树中有一条路径满足条件就行。
/**
* 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==NULL) return false; if(root->left==NULL&&root->right==NULL&&root->val==sum)
return true; return hasPathSum(root->left,sum-root->val)||hasPathSum(root->right,sum-root->val) ;
}
};
方法二:利用后续遍历的思想
减值的过程类似后续遍历中的方法二。具体过程:先沿左子树的左孩子不停的将左孩子重复入栈,并计算和栈中节点的和,直到左孩子为空,若为叶节点且val=sum,则返回true,否则再转向右孩子。定义变量pre防止重复的访问右子树,每次出栈时,特别要注意的是,要将cur赋值为NULL这样可以跳过while循环,避免重复访问左孩子。具体代码如下:
/**
* 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)
{
stack<TreeNode *> stk;
TreeNode *pre=NULL;
TreeNode *cur=root;
int temVal=; while(cur|| !stk.empty())
{
while(cur)
{
stk.push(cur);
temVal+=cur->val;
cur=cur->left;
}
cur=stk.top();
if(cur->left==NULL&&cur->right==NULL&&temVal==sum)
return true;
if(cur->right&&cur->right !=pre)
cur=cur->right;
else
{
stk.pop();
temVal-=cur->val;
pre=cur;
cur=NULL;
} }
return false;
}
};
[Leetcode] Path Sum路径和的更多相关文章
- [leetcode] Path sum路径之和
要求给定树,与路径和,判断是否存在从跟到叶子之和为给定值的路径.比如下图中,给定路径之和为22,存在路径<5,4,11,2>,因此返回true;否则返回false. 5 / \ 4 8 / ...
- 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 III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [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 二叉树的路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [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] 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] 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 II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
随机推荐
- python 摘要算法
一.概述: 摘要算法主要特征是加密过程不需要密钥,并且加密的数据无法解密,只有输入相同的明文数据经过相同的摘要算法才能得到相同的密文.摘要算法主要应用在“数字签名”领域.接下来会讲述RSA公司的MD5 ...
- Centos7安装FastDFS
离线安装包准备: 将相关的安装包上传到 /usr/local 目录,安装包下载 并解压到当前目录 1.安装 gcc yum install -y gcc gcc-c++ 2.安装 perl yum i ...
- ZooKeeper(2)-安装和配置
一.下载 https://zookeeper.apache.org/ 二.本地模式安装 1.安装前准备 (1)安装Jdk (2)拷贝Zookeeper安装包到Linux系统下 (3)解压到指定目录 . ...
- 11 非阻塞套接字与IO多路复用(进阶)
1.非阻塞套接字 第一部分 基本IO模型 1.普通套接字实现的服务端的缺陷 一次只能服务一个客户端! 2.普通套接字实现的服务端的瓶颈!!! accept阻塞! 在没有新的套接字来之前,不能处理已经建 ...
- 怎么用Python Flask模板jinja2在网页上打印显示16进制数?
问题:Python列表(或者字典等)数据本身是10进制,现在需要以16进制输出显示在网页上 解决: Python Flask框架中 模板jinja2的If 表达式和过滤器 假设我有一个字典index, ...
- 关于VSCode如何缩进两个空格
使用VSCode编写vue的时候,由于缩进问题经常报错.(默认缩进4个空格,实际规范上是两个空格) 更改VSCode的缩进格式. 但是此时你在编写代码的时候却发现任然缩进4格,此时因为vscode默认 ...
- C语言数据结构(二)
算法和算法的衡量 一.算法 算法是为了解决某类问题而规定的一个有限长的操作序列.一个算法必须满足以下五个重要特性: 1.有穷性 对于任意一组合法输入值,在执行又穷步骤之后一定能结束,即:算法中的每 ...
- Tomcat配置SSL连接
1.服务器端单项认证 在Tomcat的server.xml文件中,已经提供了现成的配置SSL连接器的代码,只要把<Connector>元素的注释去掉即可: <!— Define a ...
- PHP.41-TP框架商城应用实例-后台16-商品属性2-AJAX添加、删除
添加商品属性 思路:根据[后台15]类型表{id,type_name}与属性表{id,attr_name,attr_type,attr_option_values,type_id} 1.建表商品属性 ...
- 通过调用API在JavaWeb项目中实现证件识别
本文详细介绍自己如何在JavaWeb项目中通过调用API实现证件识别. 一,Face++使用简介 二,两种方式(图片URL与本地上传)实现证件识别 一,Face++使用简介 Face++旷视人工智能开 ...