LeetCode OJ--Path Sum *
https://oj.leetcode.com/problems/path-sum/
树的深搜,求从根到叶子的路径。
记住深搜的样子
#include <iostream>
using namespace std;
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) {
//null
if(root == NULL )
return false; //leaf node
if(root->left == NULL && root->right == NULL && root->val == sum)
return true;
if(root->left == NULL && root->right == NULL)
return false; //not leaf node
if(root->left ||root->right)
{
bool ans = false; if(root->left)
{
ans = hasPathSum(root->left, sum - root->val);
if(ans == true)
return true;
}
if(root->right)
{
ans = hasPathSum(root->right, sum-root->val);
if(ans == true)
return true;
}
return false;
}
return false;
}
}; int main()
{
TreeNode *n1 = new TreeNode();
TreeNode *n2 = new TreeNode(-);
TreeNode *n3 = new TreeNode(-);
TreeNode *n4 = new TreeNode();
TreeNode *n5 = new TreeNode();
TreeNode *n6 = new TreeNode(-);
TreeNode *n7 = new TreeNode(-);
n1->left = n2;
n1->right = n3;
n2->left = n4;
n2->right = n5;
n3->left = n6;
n3->right = NULL;
n4->left = n7;
class Solution myS;
cout<<myS.hasPathSum(n1,);
return ;
}
LeetCode OJ--Path Sum *的更多相关文章
- [LeetCode] 437. Path Sum III_ Easy tag: DFS
		
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
 - [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] 113. Path Sum II 路径和 II
		
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
 - [LeetCode] 437. Path Sum III 路径和 III
		
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
 - [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 437. Path Sum III (路径之和之三)
		
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
 - [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
		
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
 - [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 ...
 - 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance
		
引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...
 - [Leetcode Week14]Path Sum II
		
Path Sum II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/path-sum-ii/description/ Description Giv ...
 
随机推荐
- 【最大权闭合子图  最小割】bzoj1497: [NOI2006]最大获利
			
最大权闭合子图的模型:今天才发现dinic板子是一直挂的…… Description 新的技术正冲击着手机通讯市场,对于各大运营商来说,这既是机遇,更是挑战.THU集团旗下的CS&T通讯公司在 ...
 - Helm入门
			
前言:Helm是GO语言编写的,是管理kubernetes集群中应用程序包的客户端工具.Helm是类似于centos上的yum工具或Ubuntu上的apt-get工具.对于应用发布者而言,可以通过He ...
 - python 监控日志
			
#需求: #1.每分钟监控服务器日志,ip请求超过200次的,加入黑名单 #1.读文件,获取到每行的内容 open readlines # 178.210.90.90 - - [04/Jun/2017 ...
 - ActiveXObject
			
只有IE浏览器才支持这个构造函数,可以用这个来判断,当前是否为IE浏览器 var isIE=!!window.ActiveXObject; 在IE的不同版本下,要创建XHR对象,也需要通过这个构造函数 ...
 - Nastya Studies Informatics CodeForces - 992B (大整数)
			
B. Nastya Studies Informatics time limit per test 1 second memory limit per test 256 megabytes input ...
 - UVALive - 3942 (DP + Trie树)
			
给出一个长度不超过300000的字符串 S,然后给出 n 个长度不超过100的字符串. 如果字符串可以多次使用,用这 n 个字符串组成 S 的方法数是多少? 比如样例中,abcd = a + b + ...
 - Linux学习-备份策略
			
每部主机的任务都不相同,重要的数据也不相同,重要性也不一样,因此,每个人的备份思考角度都不一样! 备份分为两大部分,一个是每日备份经常性变动的重要数据, 一个则是每周备份就不常变动的信息.这个时候我就 ...
 - xml了解
			
Xml简介 ➢XML指可扩展标记语言(Extensible Markup Language) ➢XML被设计用于结构化.存储和传输数据 ➢XML是一种标记语言,很类似于HTML ➢XML没有像HTML ...
 - python学习-- for和if结合使用
			
for和if结合使用: <h1> {% for i in contents %} {{ i }}{# 注意i也要用两个大括号 #} {% endfor %} </h1> < ...
 - Redis 使用多个数据库及密码配置
			
redis的默认端口是6379,可以使用的数据库最多有16个,不同数据库之间是独立的, 可以通过 select num 的方式访问不同的数据库 可以通过下面的命令来切换到不同的数据库下,每个数据库都有 ...