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. unity导入3dsMax源文件.max

    https://blog.csdn.net/qq_28002559/article/details/53693621 首先unity导入3dsMax文件为什么要使用.max而不用.fbx,因为我不是做 ...

  2. 【转】一步一步教你在Ubuntu12.04搭建gstreamer开发环境

    原文网址:http://blog.csdn.net/xsl1990/article/details/8333062 闲得蛋疼    无聊寂寞冷    随便写写弄弄 看到网上蛮多搭建gstreamer开 ...

  3. linux(7)

    第十七单元 Samba服务 [本节内容]1. 掌握samba的功能: samba是一个网络服务器,用于Linux和Windows之间共享文件.2. 掌握samba服务的启动.停止.重启service ...

  4. VBA7种遍历方法

    Sub 在选定文档最后加入一句话() '遍历文件 Dim MyDialog As FileDialog On Error Resume NextApplication.ScreenUpdating = ...

  5. mac下的安装神奇 brew --例子 安装 mysql

    da打开终端 输入bre 输入 bre search mysql (查找mysql版本) 输入 bre install mysql@5.6(选择mysql版本安装)

  6. 阻塞队列之一:BlockingQueue汇总

    一.阻塞队列介绍 BlockingQueue 通常用于一个线程生产对象,而另外一个线程消费这些对象的场景.下图是对这个原理的阐述: 一个线程往里边放,另外一个线程从里边取的一个 BlockingQue ...

  7. Java测试用例简介

    最近需要向组内其他成员普及一下关于Java测试用例的相关知识,特在此进行一下简单的学习和总结. JUnit简介 JUnit是一个开源的Java单元测试框架,JUnit4对原有的JUnit框架进行了大幅 ...

  8. 学生党如何拿到阿里技术offer:《阿里面试经历-2014.4.18研发实习生面试经历(失败)》

    我们分享的上一篇文章是一位学长在大三的时候面试阿里实习生成功的经历的分享,其实就像学长在上一篇文章最后说的那样“面试并没有想的那么难,运气也会占一部分.”,其实我个人觉得,对于我们而言,自己越努力就会 ...

  9. WP8 MVVM设计模式

    类似了Android里边的MVC模式, Windows Phone 有自己的Model-View-ViewModel模式,这种模式的作用就是为了Data和UI分离开来. 如果你英文较好的话,你可以不再 ...

  10. python开发_function annotations

    在看python的API的时候,发现了一个有趣的东东,即:python的方法(函数)注解(Function Annotation) 原文: 4.7.7. Function Annotations Fu ...