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. zabbix 布署实践【8 监控windows server】

    参考http://www.cnblogs.com/likehua/p/3968689.html的思路,我安装的是zabbix 3.0 从zabbix官网下载windown的 Zabbix pre-co ...

  2. golang的sync包例子

    package main import ( "fmt" "sync" ) var wg sync.WaitGroup func asyncTestFunc() ...

  3. golang调用动态库

    测试动态库 test_so.h int test_so_func(int a,int b); test_so.c #include "test_so.h" int test_so_ ...

  4. LogUtils日志管理工具

    public class LogUtils { public static final int VERBOSE = 1; public static final int DEBUG = 2; publ ...

  5. Jetty实战(杂七杂八)

    最近开始选择JETTY作为服务器了,乘这现在空闲点学习了些JETTY的部署知识,原来她真的跟TOMCAT很类似,先总结如下: 部署应用方法(下载好jetty); 方法一: 直接将应用的 war包放在j ...

  6. vue 跟路径加载缺少跟前缀

    vue 加载资源失败:跟路径残缺,都是配置时 一个正斜杠 / 多余惹的祸

  7. [转]MySQL 经验集

    -- my.ini -> 在 [mysqld] 节点下加入一行 skip-grant-tables 然后重启服务 -- 接下来无密码登录到 mysql 执行以下命令 use mysql show ...

  8. jQuery笔记——UI

    jQuery UI 的官网网站为:http://jqueryui.com/,我们下载最新版本的即可,使用JQueryUI中的样式比我们使用原生的HTML要好看,还会有一些封装好的特效,JQueryUI ...

  9. java死锁及解决方案

    死锁是两个甚至多个线程被永久阻塞时的一种运行局面,这种局面的生成伴随着至少两个线程和两个或者多个资源.避免死锁方针:a:避免嵌套封锁:这是死锁最主要的原因的,如果你已经有一个资源了就要避免封锁另一个资 ...

  10. ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var mysql (转)

    ps -A | grep -i mysql kill 列出来的进程 service mysql start 我的问题就解决了 ------------------------------------- ...