[leetcode] 4. 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 1return true, as there exist a root-to-leaf path
5->4->11->2which sum is 22.
首先这个树很奇怪。。。。我也不知道他是怎么插进去的值的,不像传统二叉树那样有大小判断插入,而是好像在。。。随机插入。。。当然这个我们不管,题目是要问找一条从根到叶的路径加下来sum能否等于给的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:
void PreSum(TreeNode *temp, int sum, int tmp, bool &flag)
{
if (temp != NULL)
{
if (temp->val + tmp == sum && temp->left == NULL && temp->right == NULL)
flag = true;
else
{
tmp += temp->val;
PreSum(temp->left, sum, tmp, flag);
PreSum(temp->right, sum, tmp, flag);
}
} } bool hasPathSum(TreeNode *root, int sum) {
bool flag = false;
PreSum(root, sum, 0, flag);
return flag;
}
};
因为这个递归弹出实在没法跟要求一样,所以我加了个flag作为判断。
[leetcode] 4. 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 ...
随机推荐
- 新浪微博Oauth2.0授权 获取Access Token
新浪微博开放平台提供了丰富的API接口,利用这些接口,开发者能够开发出独具特色的微博应用.但是,大部分接口都需要用户授权给应用,应用利用授权得到的Access Token来调用相应的接口来获取内容. ...
- Form Data 和 Request Payload 区别
Form Data 和 Request Payload 区别 如果请求头里设置Content-Type: application/x-www-form-urlencoded,那么这个请求被认为是表单请 ...
- sysbench基准测试(2)——oltp.lua测试
前面知道sysbench基准测试的主要步骤为:prepare(准备数据集)→ run(运行测试)→ cleanup(清除数据集) 这一节介绍oltp.lua测试. oltp基准测试模拟了一个简单的事物 ...
- leetcode36
public class Solution { public bool IsValidSudoku(char[,] board) { ; i < ; i++) { var dic = new D ...
- 深入理解Javascript中构造函数和原型对象的区别(转存)
Object是构造函数,而Object.prototype是构造函数的原型对象.构造函数自身的属性和方法无法被共享,而原型对象的属性和方法可以被所有实例对象所共享. 首先,我们知道,构造函数是生成对象 ...
- 塔防游戏 Day3
1. 添加按钮动画 选择 Button->Transition 为 Animation ,然后自定义四种状态动画即可. 2. 控制升级面板的显示和隐藏 // 升级处理 // 若点击同一炮塔,并且 ...
- jquery 报错 Uncaught TypeError: Illegal invocation
遇到这个错误 请检查你的ajax提交方法的参数 1 参数是否都有定义 2 参数个数是否一致 3参数是否都有值(******)
- 在Ajax请求中什么时候用GET方式什么时候用POST方式?
当我们有大量数据要传送时最好的办法是一次发出多个只传递少量信息的Ajax调用时.如果你正用一个Ajax调用发送大量数据,那么最好是结束这种做法,因为这样做并不能节约时间. 因此,需要传送大量数据能成为 ...
- REST介绍与REST在PHP中的应用
当HTTP被发明出来的时候,其实REST就已经存在了.可惜这么多年来,WEB开发模式却越来越背离HTTP的本质,舍本逐末的追求起RPC之类的东西.此时REST重新回到人们的视线里,无疑让大家开始反思过 ...
- SaltStack 运行机理特点
SaltStack 运行机理特点 1.实时通讯(REAL-TIME COMMUNICATION) 所有 Salt minions 接收命令都是同时的,这意味着控制 10 个或者 10000 个系统所消 ...