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


下午做了个笔试没睡觉,晚上整个人都不好了,一点刷题的感觉都没有。很容易想到用深度有限搜索。开始想用栈实现,结果写的乱七八槽,后来才改成用递归实现深搜。用数组path记录从根节点到当前的路径,如果当前节点是叶节点并且找到合适的路径,就把path转成vector放入结果的二维vector中;如果当前不是叶节点,就假定它在路径上,把它放入path中,并且把sum减掉当前节点的val,供递归时候使用。代码如下:
 1 #include <iostream>2 #include <vector>3 #include <stack>4 using namespace std;5 6 struct TreeNode {7     int val;8     TreeNode *left;9     TreeNode *right;
10     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
11 };
12
13 class Solution {
14 private:
15     int path[10000];
16     vector<vector<int> > answer;
17 public:
18     vector<vector<int> > pathSum(TreeNode *root, int sum) {
19         dfs(root,sum,0);
20         return answer;
21     }
22     void dfs(TreeNode* root,int sum,int path_index){
23         if(root == NULL)
24             return;
25         if(root->val == sum && root->left == NULL && root->right == NULL)
26         {
27             //找到一条路径
28             vector<int> temp;
29             for(int i= 0;i < path_index;i++)
30                 temp.push_back(path[i]);
31             temp.push_back(sum);//叶节点这里才进入向量
32             answer.push_back(temp);
33         }
34         else{
35             sum -= root->val;
36             path[path_index++] = root->val;
37             if(root->left != NULL)
38                 dfs(root->left,sum,path_index);
39             if(root->right != NULL)
40                 dfs(root->right,sum,path_index);
41         }
42     }
43 };
44 int main(){
45     TreeNode* treenode = new TreeNode(5);
46     TreeNode* left = new TreeNode(4);
47     treenode->left = left;
48     TreeNode* right = new TreeNode(8);
49     treenode->right = right;
50
51     Solution s;
52     vector<vector<int> > a = s.pathSum(treenode,9);
53     for(int i = 0;i < a.size();i++){
54         std::vector<int> v = a[i];
55         for(int j = 0;j < v.size();j++)
56             cout << v[j]<<" ";
57         cout <<endl;
58     }
59
60 }

转载于:https://www.cnblogs.com/sunshineatnoon/p/3737613.html

【leetcode】Path Sum II相关推荐

  1. [LeetCode]113.Path Sum II

    [题目] Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the giv ...

  2. LeetCode 113. Path Sum II

    113. Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum eq ...

  3. 【Leetcode】213. 打家劫舍II(House Robber II)

    Leetcode - 213 House Robber II (Medium) 题目描述:一个小偷沿着一条环形的街偷窃,给定数组表示每家屋子的金额,如果偷窃连续的两间屋子就会触发警报,求在不触发警报的 ...

  4. Leetcode: 113. Path Sum II

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

  5. LeetCode OJ - Path Sum II

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

  6. 【LeetCode】Maximize Sum Of Array After K Negations(K 次取反后最大化的数组和)

    这道题是LeetCode里的第1005道题. 题目描述: 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个个索引 i 并将 A[i] 替换为 -A[i],然后总共重复这个过程 K 次. ...

  7. 【Leetcode】2281. Sum of Total Strength of Wizards

    题目地址: https://leetcode.com/problems/sum-of-total-strength-of-wizards/ 给定给一个长nnn数组AAA,对于每个子数组aaa,求f(a ...

  8. 【LeetCode】275. H-Index II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/h-index- ...

  9. 【LeetCode】Basic Calculator II

    Basic Calculator II 问题描述 Implement a basic calculator to evaluate a simple expression string. The ex ...

最新文章

  1. 编写EasyCluster V2.0 Portal主界面时的HTML心得(NOWRAP)
  2. 说说报表工具的无编码定制能力
  3. 入坑-DM导论-第一章绪论笔记
  4. python正则匹配所有的中文,数字和英文
  5. css中变量_CSS中的变量
  6. java经典笔试题目_java笔试考题(经典).pdf
  7. Ubuntu系统日志分析
  8. java 判断是否为cst格式_Java判断文件编码格式
  9. 关于如何用centos7和阿里云服务器去创建一个网站
  10. 计算机无法打印 重启又好了,打印机显示通讯错误,不能打印,但电脑重启后又好了!这是为什么?...
  11. 笔记本可自行更换CPU、独显了,老外用它手搓了台“PS5”
  12. RTKLIB ubuntu compile
  13. 输入框采用自动填充数据后,变成浅黄色背景
  14. React.js -学习总结1
  15. GraphicsLab Project之光照贴图烘焙(一)
  16. spingioc浅见
  17. 关于无线通信的简单理解
  18. TMD,被下属拒绝麻了...
  19. 统计信号处理小作业——瑞利分布噪声中确定性直流信号的检测
  20. 二次元日系游戏制作工具 - live2dSDK项目实战

热门文章

  1. 微信小程序——云服务环境的配置
  2. javaweb学习总结(四十五)——监听器(Listener)学习二
  3. vue --- [全家桶]vue-router
  4. javascript --- 类、class、事件委托的编程风格
  5. Webpack进阶(一) tree shaking与不同mode
  6. 大数据告诉你,电商都把假货发给谁?
  7. 调查:Java程序员最伤心,C++程序员最年老
  8. WEB-INFO/lib build path 的jar包问题
  9. Spring jdbc 对象Mapper的简单封装
  10. Java二元运算和三元运算速度测试