112. Path Sum (Tree; DFS)
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
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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(!root) return false;
flag = false;
target = sum;
preOrder(root,);
return flag; }
void preOrder(TreeNode* node,int sum){
sum = node->val + sum; //递归前,加上当前节点
if(node->left)
{
preOrder(node->left,sum);
}
if(node->right)
{
preOrder(node->right,sum);
}
if(!node->left && !node->right && sum == target) //递归结束条件:到了叶子节点
{
flag = true;
}
}
private:
bool flag;
int target;
};
112. Path Sum (Tree; DFS)的更多相关文章
- 124. Binary Tree Maximum Path Sum (Tree; DFS)
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...
- leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III
112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...
- [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 OJ 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] 112. Path Sum_Easy tag: DFS
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- LeetCode 112 Path Sum(路径和)(BT、DP)(*)
翻译 给定一个二叉树root和一个和sum, 决定这个树是否存在一条从根到叶子的路径使得沿路全部节点的和等于给定的sum. 比如: 给定例如以下二叉树和sum=22. 5 / \ 4 8 / / \ ...
- [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 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- [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 ...
随机推荐
- selectToUISlider
版权声明:本文为LZUGIS原创文章,未经同意不得转载. https://blog.csdn.net/GISShiXiSheng/article/details/24304321 用过google e ...
- 高并发的epoll+线程池,线程池专注实现业务
我们知道,服务器并发模型通常可分为单线程和多线程模型,这里的线程通常是指“I/O线程”,即负责I/O操作,协调分配任务的“管理线程”,而实际的请求和任务通常交由所谓“工作者线程”处理.通常多线程模型下 ...
- CF 1093E Intersection of Permutations——CDQ分治
题目:http://codeforces.com/contest/1093/problem/E 只能想到转化成查询一个区间里值在一个范围里的数的个数…… 没有想到这样适合用主席树套树状数组维护.不过据 ...
- BZOJ3261:最大异或和
浅谈\(Trie\):https://www.cnblogs.com/AKMer/p/10444829.html 题目传送门:https://lydsy.com/JudgeOnline/problem ...
- mstsc遇到CredSSP加密Oracle修正
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\P ...
- HDOJ5875(线段树)
Function Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- android视频处理相关资料
<开源>基于Android的点对点视频通信/RTSP/RTP/H.264 http://blog.csdn.net/cazicaquw/article/details/8650543 历经 ...
- postman断言的方法
1.在test添加断言 2.检查response的body中是否包含字符串: tests["Body matches string"] = responseBody.has(&qu ...
- Java 打印一个心心
package Day8_06; public class For { public static void main(String[] args) { System.out.println(&quo ...
- Python中断多重循环的几种思路exit_flag
常见方法 II. 跳出多重循环 事实上,Python的标准语法是不支持跳出多重循环的,所以只能利用一些技巧,大概的思路有:写成函数.利用笛卡尔积.利用调试. 写成函数 在Python中,函数运行到re ...