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.

Note: A leaf is a node with no children.

Example:

Given the below binary tree and sum = 22,

     / \

   /   / \

 /  \      \
          

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

方法一:利用深度优先搜索DFS的方法来遍历每一条完整的路径,利用递归方法不停地找子节点的左右结点。首先,若输入的是空节点,则返回false,若是为叶子结点且其值为当下的值时,则返回true,否则,只要子节点的左右结点中任一结点可行则返回true。(C++)

 bool hasPathSum(TreeNode* root, int sum) {
if(!root)
return false;
if(root->val==sum&&!root->left&&!root->right)
return true;
return hasPathSum(root->left,sum-root->val)||hasPathSum(root->right,sum-root->val);
}

方法二:使用遍历的方法,每一个结点都加上其父结点的值,如果到叶子结点时,其值等于sum,就存在一条符合题意的路径,在这道题中,不一定非要右结点先入栈,左右顺序不影响结果。(C++)

  bool hasPathSum(TreeNode* root, int sum) {
if(!root)
return false;
stack<TreeNode*> s{{root}};
while(!s.empty()){
TreeNode* tmp=s.top();
s.pop();
if(!tmp->left&&!tmp->right){
if(tmp->val==sum)
return true;
}
if(tmp->right){
tmp->right->val+=tmp->val;
s.push(tmp->right);
}
if(tmp->left){
tmp->left->val+=tmp->val;
s.push(tmp->left);
}
}
return false;
}

LeetCode 112. Path Sum 二叉树的路径和 C++的更多相关文章

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

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

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

  5. [LeetCode] Path Sum 二叉树的路径和

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  6. (二叉树 DFS 递归) 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 ...

  7. [leetcode]124. Binary Tree Maximum Path Sum二叉树最大路径和

    Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...

  8. 112. Path Sum二叉树路径和

    [抄题]: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...

  9. LeetCode 437. Path Sum III (路径之和之三)

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

随机推荐

  1. p132程序代码解析

    1.  long before = System.currentTimeMillis(); ...... long after = System.currentTimeMillis(); 解析:该两句 ...

  2. javascript获取某个月份的天数

    function DayNumOfMonth(Year,Month)      //例DayNumOfMonth(2017,05);{ Month--;   //月份是0~11.month=4 var ...

  3. 对于Vue的v-if 和 v-showi

    V-if   : 1. 是条件渲染,因为它会确保在切换过程中条件块内的事件监听器和子组件适当的被销毁和重建. 2. 是惰性的,如果初始渲染条件为假,则什么都不做,直到条件第一次变为真的时候,开开始渲染 ...

  4. P2257 莫比乌斯+整除分块

    #include<bits/stdc++.h> #define ll long long using namespace std; ; int vis[maxn]; int mu[maxn ...

  5. 钱管够,你能接这个项目吗?+ tomcat源码分析

    最近看了几个咕泡学院的公开课,课堂老师讲到下面这两个经历. 1:钱给够,你有没有能力接下这个全国性的项目 平时也会有怀才不遇的时候,但是当你遇到这个机会的时候,有没有信心去接下这个单子呢? 信心和能力 ...

  6. MySQL 表字段操作

    MySQL 表字段操作 一.增加表字段 1)mysql> alter table 二.删除表字段 三.修改表字段

  7. Reactor和Proactor

    服务器编程框架 IO处理单元 :处理用户连接,读写网络数据:(单机) :作为接入服务器,实现负载均衡;(集群) 请求队列 :各个单元之间通信的抽象,通常被实现为池的一部分:一个单元通知另外一个单元,或 ...

  8. 我发起了一个 用 C# 写 的 浏览器 开源项目 HtmlCore

    我之前还发起过一个 项目, 名字也叫 HtmlCore, 见 <我发起了一个 .Net 开源 跨平台 GUI (界面开发框架)项目 HtmlCore>  https://www.cnblo ...

  9. 页面滚动图片等元素动态加载插件jquery.scrollLoading.js

    如果一个网页很长,那么该页面的加载时间也会相应的较长.而这里给大家介绍的这个jQuery插件scrollLoading的作用则是,对页面元素进行动态加载,通俗的说就是滚到哪就加载到哪,屏幕以下看不见的 ...

  10. nil和Nil及NULL的区别(仅作记录)

    今天在研究红黑树的时候一直提到一个NIL节点,百度了一下,这里仅作记录 nil是一个对象值,如果要把一个对象设置为空的时候就用nil.Nil是一个类对象的值,如果要把一个Class类型的对象设置为空的 ...