【Path Sum】cpp
题目:
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 a binary tree node.
* 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;
int next = sum - root->val;
if ( !root->left && !root->right ) return sum==root->val;
if ( root->left && !root->right ) return Solution::hasPathSum(root->left, next);
if ( !root->left && root->right ) return Solution::hasPathSum(root->right, next);
return Solution::hasPathSum(root->left, next) || Solution::hasPathSum(root->right, next);
}
};
tips:
一开始没理解好题意。
这个题要求必须走到某条path的叶子节点才算数,因此终止条件为走到叶子节点或者NULL。此外,root->left或者root->right不为NULL才往这个分支走。
============================================
第二次过这道题,终止条件是要走到叶子节点,但是参数传递写的有点儿啰嗦。
/**
* Definition for a binary tree node.
* 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)
{
bool find = false;
if(root) Solution::pathSum(root, sum, , find);
return find;
}
static void pathSum(TreeNode* root, int sum, int pathsum, bool& find)
{
if ( find ) return;
if ( !root->left && !root->right )
{
if ( (pathsum+root->val)==sum )
{
find = true;
return;
}
}
if ( root->left ) Solution::pathSum(root->left, sum, pathsum+root->val, find);
if ( root->right ) Solution::pathSum(root->right, sum, pathsum+root->val, find);
}
};
【Path Sum】cpp的更多相关文章
- 【Binary Tree Maximum Path Sum】cpp
题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...
- 【二叉树的递归】03判断二叉树中有没有和为给定值的路径【Path Sum】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树和一个和,判断这个树 ...
- 【Minimum Path Sum】cpp
题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...
- 【Combination Sum 】cpp
题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C ...
- 【Two Sum】cpp
题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...
- 【Path Sum II】cpp
题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...
- 【二叉树的递归】05二叉树中找任意起点和终点使他们的路径和最大【Binary Tree Maximum Path Sum】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,寻找值最大的路径. ...
- 【二叉树的递归】04找出二叉树中路径和等于给定值的所有路径【Path Sum II】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树和一个和,判断这个树 ...
- leetcode 【 Minimum Path Sum 】python 实现
题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...
随机推荐
- .NET Web开发总结(三)
第五章 ASP.NET 页面语法 本章详细讲解.NET页面的语法结构 一般情况下 一个ASP.NET页面要包括页面编译指令 HTML页面框架及Web窗体 服务器端控件 服务器端代码 ...
- s3c6410_时钟初始化
参考: 1)<USER'S MANUAL-S3C6410X>第三章 SYSTEM CONTROLLER 2)u-boot/board/samsumg/smdk6410/lowlevel_i ...
- IOS绘图——简单三角形
#import <UIKit/UIKit.h> @interface MyView : UIView @end #import "MyView.h" @implemen ...
- IOS多线程(一)
一.绪论 1.进程:平时看到的一个应用程序,即可算作一个线程. 每个进程都有一个PID作为进程ID,有一个Process Name作为进程名字等. 2.线程:一个进程可以有多个线程,而每个线程只可属于 ...
- 线程池(C#)
转自:http://blog.sina.com.cn/s/blog_494305f30100ryw7.html 在这里你可以学到Microsoft研究CLR实现线程池的原理机制,从而更灵活的处理CLR ...
- [原]Django调试工具--django-debug-toolbar
请摒弃简单粗暴的print --马云 我比较习惯在windows中安装pycharm开发,项目部署在虚拟机中,在本地浏览器中查看效果,这种方式在调试上会有点麻烦,django-debug-toolba ...
- 神奇的fastcgi_finish_request
当PHP运行在FastCGI模式时,PHP FPM提供了一个名为fastcgi_finish_request的方法.按照文档上的说法,此方法可以提高请求的处理速度,如果有些处理可以在页面生成完后再进行 ...
- 基于OWIN WebAPI
http://www.cnblogs.com/Irving/p/4607104.html http://www.cnblogs.com/xishuai/p/aspnet-webapi-owin-oau ...
- 6.html5分组元素
何为分组元素,首先先看下面这个例子: <span>scolia<span>scolia</span></span> <span>scolia ...
- 《Prism 5.0源码走读》UnityBootstrapper
UnityBootstrapper (abstract class)继承自Bootstrapper(abstract)类, 在Prism.UnityExtensions.Desktop project ...