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 and sum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
/**
* 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(sum-root->val== && root->left==NULL && root->right==NULL) //结束条件
return true;
if(hasPathSum(root->left,sum-root->val))
return true;
else
return hasPathSum(root->right,sum-root->val);
}
};
Path Sum 深度搜索的更多相关文章
- [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] Sum Root to Leaf Numbers dfs,深度搜索
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- 【LeetCode】Path Sum 2 --java 二叉数 深度遍历,保存路径
在Path SUm 1中(http://www.cnblogs.com/hitkb/p/4242822.html) 我们采用栈的形式保存路径,每当找到符合的叶子节点,就将栈内元素输出.注意存在多条路径 ...
- 第34-3题:LeetCode437. Path Sum III
题目 二叉树不超过1000个节点,且节点数值范围是 [-1000000,1000000] 的整数. 示例: root = [10,5,-3,3,2,null,11,3,-2,null,1], sum ...
- [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 ...
- 【LeetCode】Path Sum ---------LeetCode java 小结
Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...
- 47. leetcode 437. Path Sum III
437. Path Sum III You are given a binary tree in which each node contains an integer value. Find the ...
- LeetCode 437. Path Sum III (路径之和之三)
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
随机推荐
- c++容器中map的应用
原文链接:https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关 ...
- [转]C#截获本机数据包方法实例
本文向大家介绍Windows Sockets的一些关于用C#实现的原始套接字(Raw Socket)的编程,以及在此基础上实现的网络封包监视技术.同Winsock1相比,Winsock2最明显的就是支 ...
- 利用GitHub来进行团队协作开发项目
首先: 1.项目组长要在GitHub创建一个仓库 2.组长git clone仓库地址到本地 3.组长在本地克隆到的项目里面创建一个Django项目 4.在当前项目下进行git add以及git ...
- H5C3--文本阴影text-shadow
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 用reduce装逼 之 多个数组中得出公共子数组,统计数组元素出现次数
昨天做了一道美团的面试题,要求是给N个数组,找出N个数组的公共子数组. ,,,,]; ,,,,]; ,,,,]; ,,,,]; 以上四个数组,有公共子数组2, 3,7 function main(){ ...
- 零开始Android逆向教程(一)——初探Android逆向
这段时间因为某些业务驱动,开始研究一些逆向相关的东西,浏览了下其所包含的大致内容,发现真是一个新大陆,跟之前耳听目染过的一些门面介绍完全不是一个层级的,真正的印证了下手难这一说法. 谨此以本文开始 ...
- MVVM 一种新型架构框架
MVVM是Model-View-ViewModel的简写.微软的WPF带来了新的技术体验,如Silverlight.音频.视频.3D.动画……,这导致了软件UI层更加细节化.可定制化.同时,在技术层面 ...
- Git--版本管理的使用及理解
如果多人合作时,git也是需要中间交换服务器来解决冲突合并,这不还是集中式版本控制吗? 而svn不是也可以将所有源码下载到本机,然后在本机修改,保存在本机上,为什么这个不能说是分布式,提交的时候不也是 ...
- 在Vmware安装虚拟机WindowsServer 2003
一.创建并安装虚拟机 新建Windows2003server系统 按照下面操作即可 https://www.cnblogs.com/color-blue/p/8525710.html 二.安装虚拟机 ...
- 核K-均值聚类(Kernel K-means Clustering)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/wxcdzhangping/article/details/31366143 问题: 设 ...