[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->2
which 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 ...
随机推荐
- 08_java超市管理系统
超市管理系统功能介绍 * A:超市管理系统功能介绍 * a: 显示主菜单 ============欢迎光临ItCast超市============ 1: 货物 清单 2: 添加货物 3: 删除货物 4 ...
- Flask之模板过滤器
3.2 过滤器: 过滤器的本质就是函数.有时候我们不仅仅只是需要输出变量的值,我们还需要修改变量的显示,甚至格式化.运算等等,这就用到了过滤器. 过滤器的使用方式为:变量名 | 过滤器. 过滤器名写在 ...
- leetcode503
public class Solution { public int[] NextGreaterElements(int[] nums) { int n = nums.Length; int[] ne ...
- 左侧倒换菜单 frameset 已过时
<!doctype html><html><frameset cols="200,*"> <frame src="left.ht ...
- java获取取前段页面的参数
request.getParameter("tnumber") document.forms[0].s_id.value;
- web开发-CORS支持
一.简介 Web 开发经常会遇到跨域问题,解决方案有:jsonp,iframe,CORS 等等 1.1.CORS与JSONP相比 1.JSONP只能实现GET请求,而CORS支持所有类型的HTTP请求 ...
- C++Primer笔记-----day06
================================================================day06=============================== ...
- angularjs 出现 “Possibly unhandled rejection: cancel ”错误
Try adding this to your config. I had a similar is once and this workaround did the trick. app.confi ...
- debian:jessie 安转percona mysql
dpkg -r percona-server-server-5.6 dpkg -r percona-server-client-5.6 dpkg -r libperconaserverclient18 ...
- 【LA3523 训练指南】圆桌骑士 【双连通分量】
题意 有n个骑士经常举行圆桌会议,商讨大事.每次圆桌会议至少应有3个骑士参加,且相互憎恨的骑士不能坐在圆桌旁的相邻位置.如果发生意见分歧,则需要举手表决,因此参加会议的骑士数目必须是奇数,以防赞同和反 ...