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 ...
随机推荐
- ballerina 学习一 基本项目安装试用
ballerina介绍 建议参考这篇文章: https://mp.weixin.qq.com/s/DqdlOhquqMaGOJf26lANPw 1. 安装 直接下载对应操作系统的二进制文件即 ...
- JDK1.5java新特性
JDK1.5java增加的新特性: 自动装箱/拆箱 增强for 泛型 枚举 静态导入 可变参数 1 自动装箱/拆箱 * JDK1.5允许开发人员把一个基 ...
- springboot各种集成
fastjson 两个方法 @Configuration public class WebMvcConfigurer extends WebMvcConfigurerAdapter { @Overri ...
- ubuntu技巧
在终端界面里面查找,命令:ctrl + shift +f VMware 中 Ubuntu 不能全屏: 转到虚拟机-->安装VMware Tools 把装载的 VMware Tools CD里面的 ...
- yum命令集
升级相关命令: yum update : 安装所有更新软件 yum update xxx : 仅更新指定的软件 yum check-update : 列出所有可更新的软件清单 yum list : 列 ...
- poj 2187 Beauty Contest——旋转卡壳
题目:http://poj.org/problem?id=2187 学习材料:https://blog.csdn.net/wang_heng199/article/details/74477738 h ...
- Spring注入方式及用到的注解
注入方式: 把DAO实现类注入到service实现类中,把service的接口(注意不要是service的实现类)注入到action中,注 入时不要new 这个注入的类,因为spring会自动注入,如 ...
- Warning: skipping non-radio button in group. 的处理
1)把你的第一个Radio选中group属性: 2)把你“这个组中”的最后一个Raido控件“后”的一个控件(指得是控件添加先后的顺序,可以用ctrl+d来修改顺序)也选中group属性,如最后一个R ...
- 无法解析的外部符号 _WinMain@16
无法解析的外部符号 _WinMain@16 Ctrl+F7 编译的时候没有错误,而F6生成解决方案的时候出现如下两个错误: 1:error LNK2019: 无法解析的外部符号 _WinMain@16 ...
- Zookeeper--集群管理
Zookeeper--集群管理 在多台服务器组成的集群中,需要监控每台服务器的状态,一旦某台服务器挂掉了或有新的机器加入集群,集群都要感知到,从而采取相应的措施.一个主动的集群可以自动感知节点的死亡和 ...