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. poi 工具类

    <!--POI--> <dependency> <groupId>org.apache.poi</groupId> <artifactId> ...

  2. HOMEWORD2

    开发工具和语言选择 语言 :pyhton3.6 工具 我选择的是 pycharm2019.1.由于之前已经安装好了,这里就不再贴出. 自动单元测试练习 python单元测试框架Unittest.Uni ...

  3. cordova获取相册照片插件的使用方法:cordova-plugin-image-picker

    1. 添加插件:cordova plugin add cordova-plugin-image-picker 2.调用方法: 3.参考 : http://www.cnblogs.com/lyxy/p/ ...

  4. vue 在全局设置cookie main.js文件

    //设置cookie Vue.prototype.setCookie=function(cname, cvalue, exdays) { var d = new Date(); d.setTime(d ...

  5. TypeScript 版本相关

    TypeScript 1.3 元组类型 // Declare a tuple type var x: [string, number]; // 初始化 x = []; // ok // 错误的初始化 ...

  6. R并行计算

    # 参考文献: https://cosx.org/2016/09/r-and-parallel-computinghttps://blog.csdn.net/sinat_26917383/articl ...

  7. Java高级特性 第7节 多线程

    一.进程与线程的概念 1. 进程 进程是应用程序的执行实例,有独立的内存空间和系统资源. 如上图,标红色的是一个Office Word进程. 进程的特点: 动态性:进程是动态的创建和消亡: 并发性:操 ...

  8. docker mysql8 注意

    1. mysql8 出了有段时间了,但公司项目的django还不支持mysql8的默认加密方式. 连接时报错 Error : The server requested authentication m ...

  9. python通配符之glob模块

    转自:https://blog.csdn.net/dcrmg/article/details/78309469 官方链接:https://docs.python.org/3.6/library/glo ...

  10. Devexpress gridview cell增加控件

    1.根据上图次序,先添加三类控件类型: 2.根据上图次序设置columnEdit 3.点开ColumnEdit , 设置 check 和 uncheck的 value