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

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

思路:树的题目,整体思路就是递归查找。三行解决。

bool hasPathSum(TreeNode *root, int sum) {
if(root == NULL) return false;
if(sum == root->val && (root->left == NULL) && (root->right == NULL)) return true; //和相等 且 是叶子结点 return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);
}

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.

For example:
Given the below binary tree and sum = 22,

              5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1

return

[
[5,4,11,2],
[5,8,4,5]
]

思路:找所有路径,也是用递归。发现满足的路径就压入。

vector<vector<int> > pathSum(TreeNode *root, int sum) {
vector<vector<int> > ans;
vector<int> tmpans;
findSum(root, sum, tmpans, ans);
return ans; } void findSum(TreeNode *root, int sum, vector<int> tmpans, vector<vector<int>> &ans)
{
if(root == NULL) return;
if(sum == root->val && (root->left == NULL) && (root->right == NULL)) //满足条件 压入答案
{
tmpans.push_back(root->val);
ans.push_back(tmpans);
}
else
{
tmpans.push_back(root->val);
}
findSum(root->left, sum - root->val, tmpans, ans);
findSum(root->right, sum - root->val, tmpans, ans);
}

【leetcode】Path Sum I & II(middle)的更多相关文章

  1. 【leetcode】Linked List Cycle II (middle)

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  2. 【leetcode】Reverse Linked List II (middle)

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...

  3. 【leetcode】Search for a Range(middle)

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  4. 【leetcode】Swap Nodes in Pairs (middle)

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  5. 【leetcode】Binary Search Tree Iterator(middle)

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  6. 【leetcode】Validate Binary Search Tree(middle)

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  7. 【leetcode】Evaluate Reverse Polish Notation(middle)

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  8. 【leetcode】Container With Most Water(middle)

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  9. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

随机推荐

  1. 如何让 height:100%; 起作用

    当你设置一个页面元素的高度(height)为100%时,期望这样元素能撑满整个浏览器窗口的高度,但大多数情况下,这样的做法没有任何效果.你知道为什么height:100%不起作用吗? 按常理,当我们用 ...

  2. iOS:项目中疑难Crash问题集锦

    项目中疑难Crash问题集锦 iOS App运行中遇到Crash的情况相信大家都遇到过,开发和者测试中遇到了可能很方便的办法就是直接拿着设备连接一下,然后使用Xcode自带的工具就可以解析出Crash ...

  3. 【C语言入门教程】3.1 程序的 3 种基本结构

    程序设计是一个问题求解的过程,解决问题的步骤可看作是程序的控制结构.简单地说,程序的运行过程就是数据输入.数据处理.数据输出 3 个步骤.其中,数据处理过程是否快捷和准确,主要依赖于程序控制结构的设计 ...

  4. 修改Mysql默认编码

    show variables like 'character%';+--------------------------+----------------------------+| Variable ...

  5. 用CSS画个三角形

    <!DOCTYPE html> <html> <head> <style type="text/css"> #trangle { d ...

  6. MySQL存储引擎概述

    一.MySQL支持插件式存储引擎,默认包括有多种存储引擎,还可以自己定制化引擎,引擎是在表级别设置的. 二.各种存储引擎的特性 (A) MyISAM :不支持事务.不支持外键.访问速度快. 每个MyI ...

  7. Linux DDoS 木马再度来袭

    导读 Linux用户又有一个木马需要苦恼了,就像以往一样,这些黑客大多部署在被劫持的Linux系统上,并在接受到命令后发起DDoS攻击. 发现这件事的Dr.Web的安全研究人员说:“木马似乎是通过破壳 ...

  8. Android应用性能优化

    整理自http://androidperformance.com的几篇博客 代码内存优化-Java篇 避免创建不必须的对象,虽然GC可以回收不用的对象,但为对象分配内存和回收它们同样是需要消耗资源的. ...

  9. 热更新脚本C#light,ulua,Scorpio性能比较

    http://www.unity蛮牛.com/thread-32861-1-1.html 测试环境: unity4.5.2  三个脚本全是源码导入  PC :处理器 Intel(R) Core(TM) ...

  10. 清除SVN版本控制文件

    命名为bat后缀文件,放在对应目录下. @echo on color 2f mode con: cols=80 lines=25 @REM @echo 正在清理SVN文件,请稍候...... @rem ...