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. Python中serial的使用

    一.概述     pyserial模块封装了对串口的访问. 二.特性     在支持的平台上有统一的接口.     通过python属性访问串口设置.     支持不同的字节大小.停止位.校验位和流控 ...

  2. React Native开发的一种代码规范:Eslint + FlowType

    [这篇随笔记录的很简单,没有涉及具体的Eslint规则解释以及FlowType的类型说明和使用等,只是链接了所需的若干文档] js开发很舒服,但是代码一多起来就参差不齐,难以阅读了.所以加上一些代码规 ...

  3. c# 坑人的发邮件组件

    System.Net.Mail 在服务器25端口被封禁的情况下,无法使用其它诸如SSL 465端口发送.用过时的System.Web.Mail却可以.是微软更新速度太快呢,还是标准不一致呢. Syst ...

  4. AntV G6绘制流程图学习例子

    下面代码可以直接贴到html文件中运行看效果. 代码说明 js中data是一个json变量,里面有两个关键对象"nodes.edges",分别来描述节点.节点间箭线. 更多&quo ...

  5. MySQL数据库使用规范

    一.建表规约 1.[强制]表达是与否概念的字段,必须使用is_xxx的方式命名,数据类型是unsigned tinyint (1表示是,0表示否) 说明:任何字段如果为非负数,必须是unsigned ...

  6. Windows10 小闹钟

    Windows 10 自带小闹钟功能,便于我们进行时间管理,到点提醒. 尤其作为IT的工作者,首先要保证身体的健康,要定好休息的时间,哪怕5分钟,也能让人满血复活. 其次,便于我们将各种优先级的事务进 ...

  7. 使用 AppScan 进行扫描

    针对大型网站的扫描,我们按照戴明环 PDCA 的方法论来进行规划和讨论,建议 AppScan 使用步骤:计划(Plan).执行(Do).检查(check).分析(Analysis and Action ...

  8. 解决AndroidStudio引入Jar出现Unable to resolve dependency for ':app@debug/compileClasspath

    今天在做Android项目时遇到一个万脸懵逼的错误,表示没看懂,百度一圈说是被墙啥的 不过最终还是被朕给找到了答案,解决办法如下 点击AndroidStudio左上角 File -> setti ...

  9. vue实现原理

    1.数据监控(data):监听data属性: new Vue之后内部扫描data属性值,用 Object.defineProperty(obj,name,{ set:value=>{ obj[_ ...

  10. Java 序列化 返序列化

    原文: http://www.cnblogs.com/xdp-gacl/p/3777987.html   一.序列化和反序列化的概念 把对象转换为字节序列的过程称为对象的序列化. 把字节序列恢复为对象 ...