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. PX转REM简易计算器(适用于fittext.js插件计算)

    当你使用fittext.js插件时,通过量取的像素单位PX计算出REM单位值,是一件比较麻烦而繁琐的,为了提高工作效率,自己闲暇写了个小DEMO,现在给大家分享出来. 先看dom: <heade ...

  2. jquery的相关用法

    选择器基本选择器1.id选择器$('#id1')找到id为id1 的标签2.class选择器$('.class1')找到class中有class1这个类的标签3.标签选择器$('tag') 找到tag ...

  3. bootstrap实现列的拖动

    <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"& ...

  4. 网络操作基础(one)

    P12 一.什么是网络操作系统?网络操作系统具有哪些基本功能? 二.网络操作系统具有哪些特征? 三.常用的网络操作系统有哪些?它们各具有什么特点? 四.在网络操作系统中主要可提供哪些? ———— —— ...

  5. Temporary failure in name resolutionf的解决方法

    Linux有时还蛮烦的这个不能用那个不能用,只能多折腾了. 今天又是,ping z.cn的时候直接报错 Temporary failure in name resolutionf 这个一般都知道是DN ...

  6. cordova闪屏插件插件使用:cordova-plugin-splashscreen

    欢迎页本地插件,默认建议包含.启动本地应用时显示指定的图片(启动页) 1. 添加插件:cordova plugin add cordova-plugin-splashscreen 2. 调用方法:

  7. vue简单实例代码

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  8. c#函数地址传入c++

    c# private delegate void ValidateEvent(int eventCode); private static void ValidateEventCallback(int ...

  9. pache tomcat慢速HTTP拒绝服务攻击安全问题解决办法

    问题说明:HTTP协议的设计要求服务器在处理之前完全接收到请求.如果HTTP请求未完成,或者传输速率非常低,则服务器将保持其资源占用等待剩余的数据.如果服务器占用的资源太多,则会造成拒绝服务. 漏洞危 ...

  10. 出现No package gcc+ available解决办法

    系统 CentOS Linux release 7.4.1708 (Core)   安装gcc时报错 [root@ip---- node-v10.15.3]# yum -y install gcc+ ...