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)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. [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 ...

  4. 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 ...

  5. [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 ...

  6. LeetCode 112 Path Sum(路径和)(BT、DP)(*)

    翻译 给定一个二叉树root和一个和sum, 决定这个树是否存在一条从根到叶子的路径使得沿路全部节点的和等于给定的sum. 比如: 给定例如以下二叉树和sum=22. 5 / \ 4 8 / / \ ...

  7. [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 ...

  8. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  9. [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 ...

随机推荐

  1. 数组去重(初识ES6)

    较常见的一问题:数组去重. 方法一:利用hash数组的原理 var arr=[1,3,3,4,5,5,6,6,7,8,69,8,99,9,0,]; function unique(arry){ var ...

  2. sphinx-1.3.0扩展在pPHP 7.0.7版本编译不通过

    在这个网友也是在php7上面编译插件不通过 https://bugs.php.net/bug.php?id=71586 下php7对应的扩展文件即可 http://git.php.net/?p=pec ...

  3. flask 之 rabbit

    使用pika库操作rabbit 查找whereis rabbitmq-server 启动/usr/sbin/rabbitmq-server & 打印了一些rabbitmq服务状态信息,包括内存 ...

  4. SecureCRT上传下载文件教程(Zmodem使用教程)

    SecureCRT是一个仿真终端连接工具.它可以方便的连接SSH服务器,远程管理Linux.同时,它还能使用多种协议方便的上传下载文件.其中Zmodem是SecureCRT支持比较好的一种古老的上传下 ...

  5. couchdb的使用例子

    couchdb安装 sudo apt-get install erlang sudo apt-get install libmozjs185-dev libicu-dev 下载源码,编译安装 启动以后 ...

  6. Spring Cloud Bus 消息总线 RabbitMQ

    Spring Cloud Bus将分布式系统中各节点通过轻量级消息代理连接起来. 从而实现例如广播状态改变(例如配置改变)或其他的管理指令. 目前唯一的实现是使用AMQP代理作为传输对象. Sprin ...

  7. MSYS2环境搭建

    本机环境:Windows XP 32位MSYS2地址:http://sourceforge.net/projects/msys2/ 下载32位版本,地址:http://sourceforge.net/ ...

  8. python实战——文本挖掘+xgboost预测+数据处理+准确度计算整合版

    if __name__=="__main__": '''============================先导入数据============================= ...

  9. Java-Runoob:Java 方法

    ylbtech-Java-Runoob:Java 方法 1.返回顶部 1. Java 方法 在前面几个章节中我们经常使用到 System.out.println(),那么它是什么呢? println( ...

  10. submit提交表单

    <!DOCTYPE html><html><head> <script src="jquery-1.3.2.min.js">< ...